【发布时间】:2021-08-24 13:56:18
【问题描述】:
来自man bash:
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, the first argument
is assigned to $0 and any remaining arguments are assigned
to the positional parameters. The assignment to $0 sets the
name of the shell, which is used in warning and error messages.
我不明白$0 分配的目的。
我正在探索使用 xargs 将参数传递给多个命令的方法。以下解决方案工作得很好(灵感来自here),但我不明白为什么最后需要一个参数:
[编辑:在chepner's answer之后,参数不需要为空,它可以是任何东西,但它必须存在]。
$ cat test
abc
def
ghi
$ cat test | xargs -n 1 /bin/bash -c 'echo "1-$@"; echo "2-$@";' 'dummyArgument'
1-abc
2-abc
1-def
2-def
1-ghi
2-ghi
显然'dummyArgument' 是bash -c 能够解释$@ 所必需的(它甚至可以为空''),因为没有它我会得到以下结果:
$ cat test | xargs -n 1 /bin/bash -c 'echo "1-$@"; echo "2-$@";'
1-
2-
1-
2-
1-
2-
但是它是如何工作的呢?为什么我需要那个'dummyArgument'?
【问题讨论】:
标签: bash shell arguments command single-quotes