Jump to content

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

Recommended Posts

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


-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 with usages and need of this altogether.

Link to comment
Share on other sites

$ echo $0
$ bash -c 'echo $0'
$ bash -c 'echo $0' foo

You can use -c if you want to run a command within bash - perhaps you need bash-specific features - and you aren't already running within bash.

I'm not sure what's so complicated about that... Maybe it would be easier if you pointed out what you're confused about?

Link to comment
Share on other sites

On 6/10/2023 at 4:45 AM, polaryeti said:

I want to know a real life use case of it.

shell_exec is an example of where such functionality is used.  The function is used to run a shell command, which is done by executing the shell program with -c.  So in your code something like this:

shell_exec('someCmd && someOtherCmd >/dev/null');

Is translated into

/bin/sh -c 'someCmd && someOtherCmd >/dev/null'

and executed by the OS.

Essentially, the use case is for whenever you have a command stored in a string that you want to run.  It's not something you'd typically use directly in a terminal, but rather in a script or program.

Link to comment
Share on other sites

Bash is a shell that can provide you an interactive environment to run commands, but it also supports scripting and programming features, so the context within which you are using it is important.

Since many people find good reasons to automate tasks with scripts, the way that environment variables or command line arguments are processed is important.  

The example commands you started with, are meant to illustrate the fact that a bash shell has environment variables associated with it.  When you make a new bash shell from within a previous one, the default behavior is that you inherit environment variables from the parent shell, but otherwise, environment variables you add via export are local to the shell you have created.

Run these commands in a bash shell to see how this works.

export SHELL_NAME=one
export SHELL_NAME=two

In the particular commands you presented, the 1st command shows you the value of 3 built in environment variables.

The 2nd command is "env" which is a command that then runs the command you specify (in this case bash) with specific environment variables set or unset depending on what cli options you provided (-i in this case) or otherwise specified.  Because there are no environment variables specified with "env -i", you can see that there are no values for the $HOME and $USER environment variables, when bash is run by the env command.  Normally bash would inherit the parent shell environment variables as I illustrated above.


This variation of the command should further illustrate why the env command might be used to run a command with specific environment variables set.

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


In all cases, bash -c is needed so that you can make bash execute the quoted commands as if you had typed them interactively.  You should take a look at the bash command man page to see what arguments it will accept.  It should be obvious that if you simply tried to add additional programs with arguments on the command line, those same lines would not work the way you intend them to.  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.