为什么我的 bash 脚本的参数会导致错误?

Ale*_*lex 5 command-line scripts 12.10

为了方便上传代码到github,我创建了一个sh脚本

#commit.sh
git add .
git commit -m $1
git push origin master
Run Code Online (Sandbox Code Playgroud)

但是当我说运行它时./commit.sh "comment1 comment2 comment3",我得到以下错误:

error: pathspec 'comment2' did not match any file(s) known to git.
error: pathspec 'comment3' did not match any file(s) known to git.
Run Code Online (Sandbox Code Playgroud)

出了什么问题,我该如何使它工作?

Tim*_*uck 8

总是引用你的变量!将其更改为

git commit -m "$1"
Run Code Online (Sandbox Code Playgroud)

进而

./commit.sh "comment1 comment2 comment3"
Run Code Online (Sandbox Code Playgroud)

将工作。