-5

From Bash Manual

bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o option] [-O shopt_option] -c string [argument ...]

-c Read and execute commands from the first non-option argument after processing the options, then exit. Any remaining arguments are assigned to the positional parameters, starting with $0.

  1. Is -c an option which takes an option argument or doesn't take any option argument? I guess it takes an option argument.

    Why does it say -c Read and execute commands "from the first non-option argument" instead of "from the option argument to -c"?

    For example, in the following command, is mycommand an option argument to option -c of bash, or a non-option argument to bash?

    bash -c mycommand
    
  2. Does "Any remaining arguments" mean all the non-option arguments to bash?

    In the following command, is it correct that the non-option arguments arg1 and arg2 to bash are passed by bash to mycommand and are used as command line arguments to mycommand?

    bash -c mycommand arg1 arg2
    

    Are the following two commands equivalent? What is the difference between them?

    bash -c mycommand arg1 arg2
    bash -c 'mycommand arg1 arg2' 
    

Thanks.

1
6

Why does it say -c Read and execute commands "from the first non-option argument" instead of "from the option argument to -c"?

For example, in

bash -c mycommand

is mycommand' an option argument to option -c of bash, or a non-option argument to bash?

It is a non-option argument to bash, as you can see simply by adding more options in between:

bash -c -i 'echo 1'

In

bash -c mycommand arg1 arg2

is it correct that the non-option arguments arg1 and arg2 to bash are passed by bash to mycommand and are used as command line arguments to mycommand?

No. Again, try it:

bash -c echo arg1 arg2

Are the following two commands equivalent? What is the difference between them?

bash -c mycommand arg1 arg2
bash -c 'mycommand arg1 arg2' 

They are not equivalent. In one, mycommand arg1 arg2 runs. In the other, mycommand runs, Bash calls itself arg1, and $1 is arg2.


All of these seem like questions that would have benefitted from a slight effort of experimentation.

3
  • Thanks. (1) Do you mean that bash -c 'mycommand $0 $1' arg1 arg2 and bash -c 'mycommand arg1 arg2' are equivalent? (2) In bash -c mycommand arg1 arg2, if $0 and $1 don't appear inside mycommand, then is it correct that arg1 and arg2 are never used?
    – Tim
    Aug 2 '17 at 7:11
  • Michael, perhaps they would have benefitted from experimentation, but this one is a real question and a good one. I have used Bash for a long time and never realized that -c does not accept an argument. This is in contrast to e.g. psql, where the first non-option argument is taken as a database name (which is taken from the PGDATABASE variable if not specified). So you have to put the SQL command directly after -c in psql, but not for bash -c.
    – Wildcard
    Aug 9 '17 at 2:34
  • When I run bash -c, it says bash: -c: option requires an argument, which sounds as if -c took an option argument
    – Tim
    Apr 7 '18 at 0:19

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