当只有一个预期的时候,gitconfig别名中的回声会回显整个命令行参数列表 [英] Echos in gitconfig aliases echoing the entire list of command line arguments when only one expected
问题描述
ignore =!([!-e .gitignore]&& touch .gitignore)| echo $ 1>> .gitignore
这对我来说不是原创的,如果您搜索它,你会看到很多地方。然而,最近我开始对别名有一个奇怪的问题。我忽略的任何东西都放在.gitignore文件中并放在同一行(仅限一个空格)。
我做了一些调试,发现真正发生的情况是对 echo $ 1
的调用是回应如您所期望的那样 $ 1
,但也会将别名的整个参数串回显。
为了测试这个,我创建了一个新的别名:
eo =!echo $ 1
> git eo test
test test
> git eo test0 test1
test0 test0 test1
最后一行是最有趣的,因为它很清楚显示echo调用正在获取整套参数,而 $ 1
正在被正确评估。事实上,如果我把事情搞砸了,把 $ 1
更改为 $ 9
(并且不要填写 $ 9
),我得到:
> git eo test0 test1
test0 test1
我已经确认这发生在Git 1.8版上。 5到1.9.0,我已经确认它不会发生在Git 1.7.1版本中;不幸的是,我无法在1.7.1和1.8.5之间进行测试。
有没有人对这里发生的事情有所了解?它破坏了我的一些别名...
我认为扩展不是您所期望的。让我们从您的别名开始:
eo =!echo $ 1
$ b 我相信 git eo test
正在扩展为:
echo $ 1 test
然后展开为:
echo test test
git查看别名的典型方式是当我说eo时,我想让你运行echo $ 1并将所有参数传递给该命令。这允许你做一些事情,例如:
$ $ $ $ $ $ $ $
$! c>
仍然可以将参数传递给 git k
。如果你希望它的行为不同,你应该像下面这样通过一个shell调用来传递它:
[别名]
eo =!sh -c'echo $ 1' -
然后你会得到预期的结果。 / p>
我认为你的忽略别名,你想要的东西就像
[别名]
ignore =!sh -c'echo$ 1>> .gitignore' -
注意:您不需要触摸 For a long time, I had the following alias in my aliases file: It wasn't original to me, and if you search for it, you'll see it a lot of places. Recently, however, I've started having a weird problem with the alias. Anything I ignore gets placed twice in the .gitignore file and on the same line (with a space only). I did a bit of debugging and found that what is really happening is that the call to To test this, I made a new alias: That last line is the most interesting because it clearly shows that the echo call is getting the entire set of arguments tacked on to it while I have confirmed that this happens on Git versions 1.8.5 to 1.9.0 and I have confirmed that it DOES NOT happen in Git version 1.7.1; unfortunately, I am unable to test between 1.7.1 and 1.8.5. Does anyone have any insight to what is going on here? It breaks a few of my aliases... I think the expansion isn't what you expect. Let's start with your alias: I believe Which is then expanded to: The typical way git looks at the alias is to say when I say "eo", I want you to run "echo $1" and pass all arguments to that command. That allows you to do things like And still be able to pass arguments to Then you'll get the expected result. I think for your ignore alias, you want something like Note: you don't need to touch the file for 这篇关于当只有一个预期的时候,gitconfig别名中的回声会回显整个命令行参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!>>< / code>来工作,并且在原来的别名中,您可能打算使用
;
来代替将输出传送到回显。 / p> ignore=!([ ! -e .gitignore ] && touch .gitignore) | echo $1 >>.gitignore
echo $1
is echoing $1
as you'd expect, but also echoing the entire string of arguments to the alias. eo = !echo $1
> git eo test
test test
> git eo test0 test1
test0 test0 test1
$1
is getting evaluated correctly. In fact, if I mess with things and change $1
to $9
(and don't fill $9
), I get:> git eo test0 test1
test0 test1
eo = !echo $1
git eo test
is being expanded to:echo $1 test
echo test test
[alias]
k = !gitk
git k
. If you want this to behave differently, you should pass it through a shell invocation like this:[alias]
eo = !sh -c 'echo $1' -
[alias]
ignore = !sh -c 'echo "$1" >> .gitignore' -
>>
to work, and in your original alias you probably meant to use ;
instead of piping the output of touch into echo.