假设我的 .gitconfig 别名中有这个:
testing =!"\
echo \"hi\" \
"
如果我运行git testing 1 4它会将参数回显给我:
hi 1 4
此外,如果我使用 sh 尝试获取所有参数,它不会显示第一个参数:
testing =!sh -c 'echo "$@"'
如果我运行git testing 1 4它只会显示4.
这是什么原因?
If your sh is bash, then the Bash man page has some info:
-c string
If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.
Before you get a chance to access $@, Bash interprets the -c and will pass the subsequent arguments through as parameters to the sub shell. This means $0 will be the next value passed, which is 4 (essentially $@ and $0 are the same at this point because there is only one value).
If you run the sh with -v you will see slightly more about what is happening:
$ git testing 1 4
echo $@
4
So the echo $@ gets only 4 in its arguments. The -c will create a sub shell, and pass $@ though. Remember this contains only 4 now. This is where you are losing the 1.
我刚刚测试过:
testing = !sh -c 'echo "$@"' {};
这确实显示了所有参数。
这类似于“ find: missing argument to-exec ”
-exec命令必须以 a 结尾(;因此您通常需要键入\;或';'避免被 shell 解释)
这也可以:
[alias]
testing = "!f() { echo \"$@\"; }; f"