[英]bash aliases in xargs: “bash -c” does not pass arguments to command
为了识别“ 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.
那我在做什么错?
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.