我似乎无法使用Bash的"-c"在"-c"后带有参数的选项选项字符串 [英] I can't seem to use the Bash "-c" option with arguments after the "-c" option string

查看:88
本文介绍了我似乎无法使用Bash的"-c"在"-c"后带有参数的选项选项字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关Bash的手册页上说到-c选项:

The man page for Bash says, regarding the -c option:

-c string 如果存在-c选项,则从中读取命令 string .如果后面有论点 字符串,它们被分配给 位置参数,从 $0.

-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

但是输出仅显示以下内容,因此看起来-c字符串之后的参数未分配给位置参数.

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:

我正在运行相当古老的Bash(在Fedora 4上):

I am running a fairly ancient Bash (on Fedora 4):

[root @ dd42 trunk]#bash --version GNU bash版本3.00.16(1)-发行版(i386-redhat-linux-gnu) 版权所有(C)2004自由软件基金会,公司.

[root@dd42 trunk]# bash --version GNU bash, version 3.00.16(1)-release (i386-redhat-linux-gnu) Copyright (C) 2004 Free Software Foundation, Inc.

我真的试图用参数执行一些shell脚本.我认为-c看起来非常有前途,因此是上面的问题.我想知道如何使用eval,但我认为我无法将参数传递给eval之后的内容.我也愿意接受其他建议.

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.

推荐答案

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

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.

这篇关于我似乎无法使用Bash的"-c"在"-c"后带有参数的选项选项字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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