“bash -c 命令参数"末尾的参数的目的是什么? [英] What's the purpose of the argument at the end of "bash -c command argument"?

查看:40
本文介绍了“bash -c 命令参数"末尾的参数的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 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 赋值的目的.

I don't understand the purpose of the $0 assignment.

我正在探索使用 xargs 将参数传递给多个命令的方法.以下解决方案工作正常(灵感来自此处),但我不明白为什么最后需要一个论点:

I was exploring ways to pass arguments to multiple commands with xargs. The following solution works just fine (inspired from here), but I fail to understand why an argument is needed at the end :

[ 以下 chepner 的回答,论点不必为空,它确实可以是任何东西,但它必须存在].

[ Edit : following chepner's answer, the argument doesn't need to be empty, it can be anything indeed, but it has to exist ].

$ 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 能够解释 $@ 所必需的,(它甚至可以是空的 '') 因为没有它我会得到以下结果:

Obviously the 'dummyArgument' is necessary for bash -c to be able to interpret $@, (it can even be empty '') because without it I get the following result :

$ cat test | xargs -n 1 /bin/bash -c 'echo "1-$@"; echo "2-$@";'
1-
2-
1-
2-
1-
2-

但是它是如何工作的?为什么我需要那个 'dummyArgument' ?

But how does it work ? Why do I need that 'dummyArgument' ?

推荐答案

我不明白 $0 赋值的目的.

它的目的是让您为内联脚本命名,可能用于装饰诊断消息.没有什么有趣的.

Its purpose is to let you give your inline script a name, potentially for decorating diagnostic messages. There is nothing interesting about it.

$ cat foo.sh
echo my name is $0
$
$ bash foo.sh a b c
my name is foo.sh
$
$ bash -c 'echo my name is $0' foo a b c
my name is foo

这篇关于“bash -c 命令参数"末尾的参数的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆