What is /bin/sh -c?
Asked Answered
S

3

83

What does /bin/sh -c mean? What does -c do?

Shorthorn answered 21/10, 2010 at 7:27 Comment(0)
S
72

From the man-page of 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.

Example:

$ bash -c ls

will launch bash and execute the command ls.

/bin/sh is usually a symlink to a shell.

Sealey answered 21/10, 2010 at 7:34 Comment(5)
How is that different to without -c ?Algophobia
You 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.Sealey
/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.Shirleenshirlene
I 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.Shirleenshirlene
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 equivalentShane
H
2

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
#
Horary answered 15/3, 2021 at 12:17 Comment(2)
Well, you do not have to give password with /bin/sh.Wynn
the sequence to execute in simple quotes, thank you!Nucleotidase
Y
2

Let's break it down.

/bin/sh: This launches a Bourne shell, a basic command-line interpreter that is available on most Unix-like operating systems.

-c: This option tells the shell to read the command from the following string. It allows you to specify a command inline, without the need to write a separate script file. This option works in both sh and bash.

For example, if you run:

/bin/sh -c "echo Hello, World!"

This command tells sh to execute the echo Hello, World! string.

Yardstick answered 26/11, 2023 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.