polaryeti Posted June 10, 2023 Share Posted June 10, 2023 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. Code: 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 10, 2023 Share Posted June 10, 2023 $ echo $0 -bash $ bash -c 'echo $0' bash $ bash -c 'echo $0' foo 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? Quote Link to comment Share on other sites More sharing options...
kicken Posted June 11, 2023 Share Posted June 11, 2023 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. Quote Link to comment Share on other sites More sharing options...
polaryeti Posted June 11, 2023 Author Share Posted June 11, 2023 I see. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 12, 2023 Share Posted June 12, 2023 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 echo $SHELL_NAME bash echo $SHELL_NAME export SHELL_NAME=two echo $SHELL_NAME exit echo $SHELL_NAME 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.