Jump to content

bash -c in linux? What does it actually do?

shivajikobardan
Go to solution Solved by Eigenvektor,
5 hours ago, shivajikobardan said:

-c string — If the -c option is present, then commands are read from string.

This simply says, if you add a "-c" then the (first) string that follows the "-c" is interpreted as a command. So e.g.

bash -c "echo Hello"

will simply use the Bash shell to execute "echo Hello", causing Hello to be printed.

 

Quote

If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

This says how additional arguments are treated, meaning if you add more than one string after the "-c". Additional arguments are used to replace $0, $1, $2, … if they appear in the command.

 

For example

bash -c 'echo $0 $1' "Hello" "Test"

will run the command "echo $0 $1", replacing "$0" with "Hello" and replacing "$1" with "Test". That's all there is to it.

 

Note however, how I used single quotes around the first string, rather than double quotes. This prevents my native shell from replacing $0 and $1, and instead passes them to its command (i.e. bash) verbatim. If I use double quotes, this is printed instead (using zsh here):

zsh

This means my native shell replaces $0 and $1, causing "echo $0 $1" to turn into "echo zsh" before the string is passed to bash (via -c), which then executes "echo zsh", causing zsh to be printed.

Before you ask me to RTFM, Quoting from the manpages:

Quote

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

But it's not very clear. I've read almost all stackoverflow and stackexchange questions about it and they've just complicated it for me.

I want to know a real life use case of it. And please upvote the correct answer so that I can know it.

bash -c 'echo $SHELL $HOME $USER'
env -i bash -c 'echo $SHELL $HOME $USER'

This is an example scenario where bash -c has been used in my tutorial that I'm following. I want to know its deep meaning and mainly the need of bash -c. I asked chatgpt but it was telling me  unintelligble stuffs.

 

Link to comment
Share on other sites

Link to post
Share on other sites

It will ensure bash is the shell that executes this command instead of whatever shell the user is using. 

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

I think bash alone is what you mean. bash -c is more complicated than that. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, shivajikobardan said:

I think bash alone is what you mean. bash -c is more complicated than that. 

'bash' starts a bash shell.

'bash -c ***' starts a bash shell, executing command ***.

 

so:

7 minutes ago, shivajikobardan said:
'echo $SHELL $HOME $USER'

executes the command

 

and

7 minutes ago, shivajikobardan said:
bash -c 'echo $SHELL $HOME $USER'

executes the command in bash

Link to comment
Share on other sites

Link to post
Share on other sites

Is it that simple? Then why's the whole of internet confused on this issue?

Link to comment
Share on other sites

Link to post
Share on other sites

45 minutes ago, shivajikobardan said:

Is it that simple? Then why's the whole of internet confused on this issue?

you appear to be the first one i've ever encountered that's confused about it.

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, shivajikobardan said:

-c string — If the -c option is present, then commands are read from string.

This simply says, if you add a "-c" then the (first) string that follows the "-c" is interpreted as a command. So e.g.

bash -c "echo Hello"

will simply use the Bash shell to execute "echo Hello", causing Hello to be printed.

 

Quote

If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

This says how additional arguments are treated, meaning if you add more than one string after the "-c". Additional arguments are used to replace $0, $1, $2, … if they appear in the command.

 

For example

bash -c 'echo $0 $1' "Hello" "Test"

will run the command "echo $0 $1", replacing "$0" with "Hello" and replacing "$1" with "Test". That's all there is to it.

 

Note however, how I used single quotes around the first string, rather than double quotes. This prevents my native shell from replacing $0 and $1, and instead passes them to its command (i.e. bash) verbatim. If I use double quotes, this is printed instead (using zsh here):

zsh

This means my native shell replaces $0 and $1, causing "echo $0 $1" to turn into "echo zsh" before the string is passed to bash (via -c), which then executes "echo zsh", causing zsh to be printed.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×