似乎无法-c选项字符串后使用bash -c选项与参数 [英] Can't seem to use bash -c option with arguments after the -c option string

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

问题描述

对于bash手册页说,对于-c选项:

Man page for bash says, regarding -c option:

-c字符串结果
  如果-c选项是present,那么命令将从读
  串。如果有参数之后
  串,它们被分配给
  位置参数,从
  $ 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):

[根@ dd42干线]#的bash --version结果
  GNU bash的版本3.00.16(1)-release(I386-红帽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,但我不认为我可以通过args设置为后面的eval东西。我接受其他的建议为好。

What I am really trying to do here is to execute a bit of 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 args to the stuff that follows eval. I'm open to other suggestions as well.

推荐答案

您需要使用单引号prevent插在你执行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

或逃生的变量在双引号字符串。使用哪种可能取决于你想要把你的code的片段是什么。

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.

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

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