我似乎无法使用 Bash“-c” “-c”后带有参数的选项选项字符串

发布于 2024-08-10 13:14:27 字数 746 浏览 1 评论 0原文

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 from
string. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自控 2024-08-17 13:14:27

您需要使用单引号来防止调用 shell 中发生插值。

$ bash -c 'echo arg 0: $0, arg 1: $1' arg1 arg2
arg 0: arg1, arg 1: arg2

或者转义双引号字符串中的变量。使用哪个可能取决于您想要在代码片段中放入的内容。

You need to use single quotes to prevent interpolation happening in your calling shell.

$ bash -c 'echo arg 0: $0, arg 1: $1' arg1 arg2
arg 0: arg1, arg 1: arg2

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.

乱了心跳 2024-08-17 13:14:27

因为字符串中的“$0”和“$1”分别替换为变量#0 和#1。

尝试:

bash -c "echo arg 0: \$0, arg 1: \$1" arg0 arg1

在此代码中,两者的 $ 都是转义,因此基将其视为字符串 $ 并且不会被替换。

该命令的结果是:

arg 0: arg0, arg 1: arg1

希望这有帮助。

Because '$0' and '$1' in your string is replaced with a variable #0 and #1 respectively.

Try :

bash -c "echo arg 0: \$0, arg 1: \$1" arg0 arg1

In this code $ of both are escape so base see it as a string $ and not get replaced.

The result of this command is:

arg 0: arg0, arg 1: arg1

Hope this helps.

伴随着你 2024-08-17 13:14:27

马丁关于插值的说法是正确的:您需要使用单引号。但请注意,如果您尝试将参数传递给正在字符串内执行的命令,则需要显式转发它们。例如,如果您有一个脚本foo.sh,例如:

#!/bin/bash
echo 0:$0
echo 1:$1
echo 2:$2

那么您应该这样调用它

$ bash -c './foo.sh ${1+"$@"}' foo "bar baz"
0:./foo.sh
1:bar baz
2:

或者更一般地说bash -c '${0} $ {1+"$@"}' <命令>; [参数]...

不是这样的

$ bash -c ./foo.sh foo "bar baz"
0:./foo.sh
1:
2:

也不像这样

$ bash -c './foo.sh $@' foo "bar baz"
0:./foo.sh
1:bar
2:baz

这意味着您可以将参数传递给子进程,而无需将它们嵌入命令字符串中,也不必担心转义它们。

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:

#!/bin/bash
echo 0:$0
echo 1:$1
echo 2:$2

Then you should call it like this:

$ bash -c './foo.sh ${1+"$@"}' foo "bar baz"
0:./foo.sh
1:bar baz
2:

Or more generally bash -c '${0} ${1+"$@"}' <command> [argument]...

Not like this:

$ bash -c ./foo.sh foo "bar baz"
0:./foo.sh
1:
2:

Nor like this:

$ bash -c './foo.sh $@' foo "bar baz"
0:./foo.sh
1:bar
2:baz

This means you can pass in arguments to sub-processes without embedding them in the command string, and without worrying about escaping them.

智商已欠费 2024-08-17 13:14:27

$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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文