【发布时间】:2016-07-06 21:51:18
【问题描述】:
我为自己编写了一个自定义的 shell 脚本,让我更容易将代码提交到 Github。
最近,我想开始使用 Github 的功能,通过在提交消息中包含问题的编号来自动关闭问题:
# Would automatically close #1 on push
git add .
git commit -m "Closes issue #1 ..."
git push
但是,我的脚本的设置方式是,它使用 $* 获取所有参数,但这会自动删除 # 符号之后的任何内容,因为这是 shell 脚本中的注释。
commit() {
# Print out commands for user to see
echo "=> git add ."
echo "=> git commit -m '$*'"
echo "=> git push --set-upstream origin $current_branch"
# Actually execute commands
git add .
git commit -m "$*"
git push --set-upstream origin $current_branch
}
现在我可以在 commit 'Closes issue #1 ...' 中加上引号,但这有点烦人……我专门设置了我的脚本,这样我就可以轻松编写:commit Whatever message I want to put in here...
我查看了手册页并进行了一些搜索,但我找不到任何关于将 # 符号作为参数转义的具体问题。
这可能吗?
【问题讨论】:
-
是否使用
$@代替$*修复它? -
@TomFenech:很遗憾,没有,我也试过了,但它仍然将
#之后的所有内容视为评论。 -
它适用于我
"$*":set -- 'This' 'message' 'contains' '#2' 'and' 'more'; git commit -m "$*"
标签: bash shell parameters escaping