为了识别“xargs”中的别名,我设置了一个别名
alias xargs="xargs bash -ic"
如果我现在执行下面的代码片段,则没有参数传递给 xargs 命令。
find . -name pom.xml | xargs grep projectid
事实上,即使在这种情况下,也不会向命令传递任何参数。
bash -ic grep projectid pom.xml
bash 的文档说
-c If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, they are assigned to the positional parameters, starting with $0.
So what am I doing wrong?
bash --version
GNU bash, version 4.3.39(2)-release (x86_64-unknown-cygwin)
更新:
感谢@knittl 的投入。现在的解决方案是避免@knittl 的回答中出现所有额外的标点符号。
1.下载xargs_bash_alias.sh
2.设置别名
alias xargs="<path>/xargs_bash_alias.sh"
现在您的 xargs 命令可以识别您的其他 bash 别名。
最佳答案
您需要注意两件事。首先,正确引用:
find . -name pom.xml -print0 | xargs -0 bash -c "grep projectid"
其次,您需要以某种方式传递您的位置参数:
find . -name pom.xml -print0 | xargs -0 bash -c 'grep projectid "$@"' -
使用 -
作为 bash 的第一个参数,所以位置参数从 $1
开始,就像在普通的 shellscript 中一样。
"$@"
扩展为从 1 开始的带引号的位置参数。
由于 xargs 一次传递多个参数,您要么需要使用 "$@"
(引用!)在您的 bash 脚本中或使用 -n1
运行 xargs选项。
关于linux - xargs : "bash -c" does not pass arguments to command 中的 bash 别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152975/