我似乎无法使用 Bash“-c” “-c”后带有参数的选项选项字符串
Bash 的手册页显示,关于 -c
选项:
-c 字符串
如果存在-c
选项,则从以下位置读取命令字符串
。如果之后有争论 字符串,它们被分配给 位置参数,从$0
。
因此,鉴于该描述,我认为类似的内容应该有效:
bash -c "echo arg 0: $0, arg 1: $1" arg1
但输出仅显示以下内容,因此看起来 -c
字符串后面的参数没有分配给位置参数。
arg 0: -bash, arg 1:
我正在运行一个相当古老的 Bash(在 Fedora 4 上):
[root@dd42 trunk]# bash --version GNU bash,版本 3.00.16(1)-release (i386-redhat-linux-gnu) 版权所有 (C) 2004 自由软件基金会,Inc.
我确实在尝试执行一些带参数的 shell 脚本。我认为 -c
看起来非常有前途,因此出现了上面的问题。我想知道如何使用 eval,但我不认为我可以将参数传递给 eval 之后的内容。我也愿意接受其他建议。
The man page for Bash says, regarding the -c
option:
-c string
If the-c
option is present, then commands are read fromstring
. If there are arguments after
the string, they are assigned to the
positional parameters, starting with$0
.
So given that description, I would think something like this ought to work:
bash -c "echo arg 0: $0, arg 1: $1" arg1
but the output just shows the following, so it looks like the arguments after the -c
string are not being assigned to the positional parameters.
arg 0: -bash, arg 1:
I am running a fairly ancient Bash (on Fedora 4):
[root@dd42 trunk]# bash --version
GNU bash, version 3.00.16(1)-release (i386-redhat-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.
I am really trying to execute a bit of a shell script with arguments. I thought -c
looked very promising, hence the issue above. I wondered about using eval, but I don't think I can pass arguments to the stuff that follows eval. I'm open to other suggestions as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用单引号来防止调用 shell 中发生插值。
或者转义双引号字符串中的变量。使用哪个可能取决于您想要在代码片段中放入的内容。
You need to use single quotes to prevent interpolation happening in your calling shell.
Or escape the variables in your double-quoted string. Which to use might depend on exactly what you want to put in your snippet of code.
因为字符串中的“
$0
”和“$1
”分别替换为变量#0 和#1。尝试:
在此代码中,两者的
$
都是转义,因此基将其视为字符串$
并且不会被替换。该命令的结果是:
希望这有帮助。
Because '
$0
' and '$1
' in your string is replaced with a variable #0 and #1 respectively.Try :
In this code
$
of both are escape so base see it as a string$
and not get replaced.The result of this command is:
Hope this helps.
马丁关于插值的说法是正确的:您需要使用单引号。但请注意,如果您尝试将参数传递给正在字符串内执行的命令,则需要显式转发它们。例如,如果您有一个脚本foo.sh,例如:
那么您应该这样调用它:
或者更一般地说
bash -c '${0} $ {1+"$@"}' <命令>; [参数]...
不是这样的:
也不像这样:
这意味着您可以将参数传递给子进程,而无需将它们嵌入命令字符串中,也不必担心转义它们。
martin is right about the interpolation: you need to use single quotes. But note that if you're trying to pass arguments to a command that is being executed within the string, you need to forward them on explicitly. For example, if you have a script foo.sh like:
Then you should call it like this:
Or more generally
bash -c '${0} ${1+"$@"}' <command> [argument]...
Not like this:
Nor like this:
This means you can pass in arguments to sub-processes without embedding them in the command string, and without worrying about escaping them.
在
$0
中添加一个反斜杠(即\$0
),否则当前的 shell 在它获得之前就将$0
转义为 shell 的名称。到子外壳。Add a backslash to the
$0
(i.e.,\$0
), otherwise your current shell escapes$0
to the name of the shell before it even gets to the subshell.