简体   繁体   中英

Send string with whitespaces as git commit message using bash

I'm creating a bash script that updates a git branch, everything works fine but now i want to add the option to specify the commit message as a parameter.

I used this command first:

echo "Executing git commit..."
git commit -m "$1"

but when i sent something like this:

$ git.sh "testing commit message"

I get a bunch of errors telling me the command cannot be recognized by git.

I suppose in this case that the double quotes I added are not passing the parameter as a single string but as many, so only the first one is taken by the -m option and git tries to pass the others as commit options.

Is there a better way i can pass a multi word string as git commit message when getting it from the script arguments?

I would really appreciate any suggestion you have.

The quotes are removed when the command is evaluated by the shell.

Use something like

git commit -m "'$1'"

这对我有用:

git commit -m "$(echo $@)"

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