简体   繁体   English

使用 bash 将带有空格的字符串作为 git commit 消息发送

[英]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.我正在创建一个更新 git 分支的 bash 脚本,一切正常,但现在我想添加将提交消息指定为参数的选项。

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.我收到一堆错误,告诉我 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.我想在这种情况下,我添加的双引号没有将参数作为单个字符串传递,而是作为多个字符串传递,因此 -m 选项仅采用第一个,而 git 尝试将其他作为提交选项传递。

Is there a better way i can pass a multi word string as git commit message when getting it from the script arguments?从脚本参数中获取时,有没有更好的方法可以将多字字符串作为 git commit 消息传递?

I would really appreciate any suggestion you have.我真的很感激你的任何建议。

The quotes are removed when the command is evaluated by the shell.当 shell 评估命令时,引号将被删除。

Use something like使用类似的东西

git commit -m "'$1'"

这对我有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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