4

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.

3
  • 4
    I think your bug is not in the code you quoted, because double-quotes absolutely should protect the parameter expansion from being treated as multiple words. Commented Nov 8, 2012 at 0:45
  • 1
    What are you trying to achieve? Aliases? Commented Nov 8, 2012 at 0:46
  • 1
    also, debug your setup with an echo xxxx"$1"XXXX before the call to git. Good luck. Commented Nov 8, 2012 at 2:22

2 Answers 2

4

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

Use something like

git commit -m "'$1'"
Sign up to request clarification or add additional context in comments.

2 Comments

for me this adds single quotes to message, but I solved by adding just spaces around the variable: git commit -m " $msg "
If you use this command in an npm script, you can't do it like that. Instead, you have to use double quotes and escape them, like so: "postdeploy": "git add package.json package-lock.json && git commit -m \"Docs %npm_package_version%\""
2

This works for me:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.