Why are my bash script's arguments causing an error?

In order to make uploading the code to github easily, I created a sh script

#commit.sh
git add .
git commit -m $1
git push origin master

But when I run it by saying ./commit.sh "comment1 comment2 comment3" , I get the error of:

error: pathspec 'comment2' did not match any file(s) known to git.
error: pathspec 'comment3' did not match any file(s) known to git.

What's wrong and how do I make it work?

3

2 Answers

Always quote your variables! Change it to

git commit -m "$1"

and then

./commit.sh "comment1 comment2 comment3"

will work.

You could combine several commands in one line.

git add . && git commit -m "Your commit message" && git push origin master

which is a nice line to use as a command:

gitpush()
{
git add . && git commit -m "$1" && git push origin master
}

If you place that command in your .bash_aliases file, you can use it as follows:

gitpush "finally fixed that long-standing bug"

This command will work when every step of it works.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like