Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Let say that I want to run a command through bash like this:

/bin/bash -c "ls -l"

According to bash man page, I could also run it like this:

#               don't process arguments after this one
#               |   pass all unprocessed arguments to command
#               |   |
#               V   V
/bin/bash -c ls -- -l

except it doesn't seem to work (it seems ignored). Am I doing something wrong, or am I interpreting man page wrong?

Relevant quotes from man:

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.

and

A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

You're interpreting the man page wrong. Firstly, the part about -- signalling the end of options is irrelevant to what you're trying to do. The -c overrides the rest of the command line from that point on, so that it's no longer going through bash's option handling at all, meaning that the -- would be passed through to the command, not handled by bash as an end of options marker.

The second mistake is that extra arguments are assigned as positional parameters to the shell process that's launched, not passed as arguments to the command. So, what you're trying to do could be done as one of:

/bin/bash -c 'echo "$0" "$1"' foo bar
/bin/bash -c 'echo "$@"' bash foo bar

In the first case, passing echo the parameters $0 and $1 explicitly, and in the second case, using "$@" to expand as normal as "all positional parameters except $0". Note that in that case we have to pass something to be used as $0 as well; I've chosen "bash" since that's what $0 would normally be, but anything else would work.

As for the reason it's done this way, instead of just passing any arguments you give directly to the command you list: note that the documentation says "command_s_ are read from string", plural. In other words, this scheme allows you to do:

/bin/bash -c 'mkdir "$1"; cd "$1"; touch "$2"' bash dir file

But, note that a better way to meet your original goal might be to use env rather than bash:

/usr/bin/env -- "ls" "-l"

If you don't need any of the features that a shell is providing, there's no reason to use it - using env in this case will be faster, simpler, and less typing. And you don't have to think as hard to make sure it will safely handle filenames containing shell metacharacters or whitespace.

share|improve this answer

I’m not sure what your objective is, but if you’re simply trying to build a Rube Goldberg machine – “a contraption, invention, device or apparatus that is deliberately over-engineered or overdone to perform a very simple task in a very complicated fashion” – then try

sh -c 'ls $0' -l

or

sh -c 'ls $1' supercalifragilisticexpialidocious -l

or even

sh -c 'ls -$0' l

You should be able to understand how these work from godlygeek’s answer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.