I can't seem to use the Bash “-c” option with

2019-01-13 15:39发布

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.

标签: bash eval
4条回答
该账号已被封号
2楼-- · 2019-01-13 15:59

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.

查看更多
做自己的国王
3楼-- · 2019-01-13 16:03

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.

查看更多
Rolldiameter
4楼-- · 2019-01-13 16:03

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.

查看更多
等我变得足够好
5楼-- · 2019-01-13 16:13

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.

查看更多
登录 后发表回答