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?
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.
$? - 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.
#$ 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With