问题描述
我使用的是Git Bash v1.8.1,带有一些别名(用于测试):
$ b $ $ $ $ $ $ $ $ $ $ [别名]
ekko =!echo $ 1&& echo $ 1
ekko2 =!sh -c'echo $ 1&& echo $ 1'
但是,当我运行它们时,我看到:
> git ekko master
master
master master
和:
> git ekko2 master
(空白行)
(空白行)
行为是:
> git ekko master
master
master
我对别名相当陌生 - 我正在寻找一种方法来确保我的参数完全消耗,而不是附加到别名的末尾。一些调查表明,这种行为在Git v1.7.x周围发生了某些变化,我还没有确定如何完成这项工作:
[别名]
ekko2 =!sh -c'echo $ 1&& echo $ 1' -
执行shell命令的Git别名替代 $ n 变量,但是它们还会将所有传递给它们的参数附加到命令的末尾。因此,在第一个例子中, git ekko master 相当于 echo master&& echo master master ,它解释了它的输出。
第二个例子更接近,但是您将master传递给 sh 命令,这只是忽略了额外的参数。通过在别名的末尾添加 - ,您可以告诉 sh 以下参数适用于脚本 sh 正在执行,而不是 sh 本身。
I'm using Git Bash v1.8.1, with a few aliases (for testing):
[alias] ekko = !echo $1 && echo $1 ekko2 = !sh -c 'echo $1 && echo $1'
But when I run them, I see:
> git ekko master master master master
And:
> git ekko2 master (blank line) (blank line)
My intended behavior is:
> git ekko master master master
I'm fairly new to aliases - I'm looking for a way to ensure that my arguments are consumed entirely, and not appended to the end of the alias. Some sleuthing indicates this behavior changed somewhere around Git v1.7.x, and I haven't yet determined exactly how to accomplish this:
Git Alias - Multiple Commands and Parameters
Your ekko2 alias is really close... What you really want is this:
[alias] ekko2 = !sh -c 'echo $1 && echo $1' -
Git aliases that execute shell commands do substitute the $n variables, but they also append any arguments you pass them to the end of the command. So in your first example git ekko master is equivalent to echo master && echo master master, which explains its output.
Your second example is closer, but you're passing "master" to the sh command, which is just ignoring the extra argument. By adding - to the end of the alias, you're telling sh that the arguments that follow are intended for the script sh is executing, and not for sh itself.
这篇关于Git别名追加参数结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!