Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

eval - I can't seem to use the Bash "-c" option with arguments after the "-c" option string

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...