Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -c mean as an argument to bash?

  docker run -d ubuntu:14.04 /bin/bash -c "while true; do echo hello world;done"

  docker run -d ubuntu:14.04 /bin/bash "while true; do echo hello world; done"

I tried both.

In case 2, the container stopped immediately. So docker ps returns nothing. And docker ps -a returns just itself.

In case 1, docker ps lists the container. It is not stopped.

So what does -c flag do?

like image 348
Gibbs Avatar asked Dec 24 '14 05:12

Gibbs


People also ask

What is an argument bash?

A command line argument is a parameter that we can supply to our Bash script at execution. They allow a user to dynamically affect the actions your script will perform or the output it will generate.

What does $? Meaning in a bash script?

$? - It gives the value stored in the variable "?". Some similar special parameters in BASH are 1,2,*,# ( Normally seen in echo command as $1 ,$2 , $* , $# , etc., ) . Follow this answer to receive notifications.

What does #$ mean in bash?

#$ does "nothing", as # is starting comment and everything behind it on the same line is ignored (with the notable exception of the "shebang"). $# prints the number of arguments passed to a shell script (like $* prints all arguments). Follow this answer to receive notifications. edited Jul 9 at 13:55.


Video Answer


2 Answers

From the bash manual page:

bash interprets the following options when it is invoked:

-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.

Without the -c, the "while true..." string is taken to be a filename for bash to open.

like image 162
Bryan Avatar answered Sep 17 '22 00:09

Bryan


The -c flag tells Bash that the next argument is a string of commands to run instead of the filename of a script. In the first case, Bash executes the next argument after -c. In the second case, where the -c flag isn't passed, Bash tries to run a script called while true; do echo hello world; done but can't find that script, so it quits.

From man bash:

-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.

like image 25
Quip Yowert Avatar answered Sep 17 '22 00:09

Quip Yowert