简体   繁体   中英

How to pass arguments in double quotes inside bash script

normally use these 3 commands to push my code to repo

git add .
git commit -m $1
git push -u git@myrepo.git  master

I tried to put all 3 commands in a script and thereby execute them together

I tried with ./upload_to_github.sh "minor change"

#!/bin/bash
#upload_to_github.sh
git add .
git commit -m $1
git push -u git@myrepo.git  master

it gives error

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

I as well tried the following one but of no use

#!/bin/bash
#upload_to_github.sh
git add .
git commit -m \"$1\"
git push -u git@myrepo.git  master

which gives me the error

error: pathspec '"change\""' did not match any file(s) known to git.

both of them doesn't seem to be working. How do I pass my $1 along with double quotes inside the script?

What you are encountering is word splitting.

What you should do is:

git commit -m "$1"

And then you call your script like

./upload_to_github 'minor change'

To understand the way it works I suggest you to read this: Bash pitfall #2 which points to Word splitting and Glob articles. Read these as well.

Or, in your script use

git commit -m "$*"

and then you can invoke the script without needing to quote the arguments:

./upload_to_github this is a minor change

The $* parameter, when quoted ( "$*" ) joins all the positional parameters as a single string, using the first char of IFS (a space, unless redefined) as the separator. http://www.gnu.org/software/bash/manual/bashref.html#Special-Parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM