What does /bin/sh -c
mean? What does -c
do?
Asked
Active
Viewed 7.7k times
2 Answers
68
From the man-page of bash:
-c string
If the
-c
option is present, then commands are read fromstring
. If there are arguments after the string, they are assigned to the positional parameters, starting with$0
.
Example:
$ bash -c ls
will launch bash and execute the command ls
.
/bin/sh
is usually a symlink to a shell.

aioobe
- 413,195
- 112
- 811
- 826
-
24How is that different to without `-c` ? – voices Apr 01 '16 at 16:35
-
10You can pass a script as argument, `bash myScript` but it will open `myScript` and interpret the commands. This is not the same as `bash -c someCommand`. Compare what happens if you do `bash ls` and `bash -c ls` for instance. – aioobe Apr 01 '16 at 18:05
-
/bin/sh -c grep "cpu cores" /proc/cpuinfo | uniq | sed -e 's/[^0-9] The above command does not give desired result. Why is it so? Whereas /bin/sh -c ls | grep c works fine. I am not getting this odd behavior. It would be great if you can explain it too. – Haseeb Jadoon Aug 16 '17 at 13:21
-
3I got it. It needs to be formatted string. /bin/sh -c 'grep "$0" /proc/cpuinfo | uniq | sed -e "$1"' "cpu cores" 's/[^0-9]*//g' worked like a charm. – Haseeb Jadoon Aug 16 '17 at 13:40
-
You have two options: `bash script.sh` or `bash -c 'ls -la'`. `-c` parameter is the command with its parameters, that will be passed to the `bash` command interpreter. If your standard command interpreter is `bash`, then `ls -la` and `bash -c 'ls -la'` are equivalent – Evghenii Orenciuc Nov 16 '21 at 17:59
1
With -c (command) it returns to the first shell instead of let open a new one.
It is very similar to: sudo su -l -c 'echo "run a command and return"'
Example:
#sudo su -l knoppix -c 'echo "run a command as ${USER} and return"'
run a command as knoppix and return
#

koyaanisqatsi
- 2,585
- 2
- 8
- 15