Difference between revisions of "What is a Subshell?"
m |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | |||
{{navigation | {{navigation | ||
|previous=Use the trap statement to catch signals and handle errors | |previous=Use the trap statement to catch signals and handle errors | ||
Line 7: | Line 6: | ||
* A Subshell can be used to do parallel processing. | * A Subshell can be used to do parallel processing. | ||
* If you start another shell on top of your current shell, it can be referred to as a subshell. Type the following command to see subshell value: | * If you start another shell on top of your current shell, it can be referred to as a subshell. Type the following command to see subshell value: | ||
− | < | + | <syntaxhighlight lang="bash" >echo $BASH_SUBSHELL</syntaxhighlight> |
OR | OR | ||
− | < | + | <syntaxhighlight lang="bash" >echo "Current shell: $BASH_SUBSHELL"; ( echo "Running du in subshell: $BASH_SUBSHELL" ;cd /tmp; du 2>/tmp/error 1>/tmp/output)</syntaxhighlight> |
* Any commands enclosed within parentheses are run in a subshell. | * Any commands enclosed within parentheses are run in a subshell. | ||
==Exporting Functions and Variables== | ==Exporting Functions and Variables== | ||
A subshell does not inherit a variable's setting. Use the [[export command]] to export variables and functions to subshell: | A subshell does not inherit a variable's setting. Use the [[export command]] to export variables and functions to subshell: | ||
− | < | + | <syntaxhighlight lang="bash" >WWWJAIL=/apache.jail |
export WWWJAIL | export WWWJAIL | ||
die() { echo "$@"; exit 2; } | die() { echo "$@"; exit 2; } | ||
export -f die | export -f die | ||
# now call script that will access die() and $WWWJAIL | # now call script that will access die() and $WWWJAIL | ||
− | /etc/nixcraft/setupjail -d cyberciti.com</ | + | /etc/nixcraft/setupjail -d cyberciti.com</syntaxhighlight> |
* However, environment variables (such as $HOME, $MAIL etc) are passed to subshell. | * However, environment variables (such as $HOME, $MAIL etc) are passed to subshell. | ||
==Use exec command to avoid subshell== | ==Use exec command to avoid subshell== | ||
You can use the [[exec command]] to avoid subshell. The exec command replaces this shell with the specified program without swapping a new subshell or proces. For example, | You can use the [[exec command]] to avoid subshell. The exec command replaces this shell with the specified program without swapping a new subshell or proces. For example, | ||
− | < | + | <syntaxhighlight lang="bash" >exec command |
# redirect the shells stderr to null | # redirect the shells stderr to null | ||
− | exec 2>/dev/null</ | + | exec 2>/dev/null</syntaxhighlight> |
==The . (dot) Command and Subshell== | ==The . (dot) Command and Subshell== | ||
The . (dot) command is used to run shell scripts as follows: | The . (dot) command is used to run shell scripts as follows: | ||
− | < | + | <syntaxhighlight lang="bash" >. script.sh</syntaxhighlight> |
The dot command allows you to modify current shell variables. For example, create a shell script as follows called /tmp/dottest.sh: | The dot command allows you to modify current shell variables. For example, create a shell script as follows called /tmp/dottest.sh: | ||
− | < | + | <syntaxhighlight lang="bash" >#!/bin/bash |
echo "In script before : $WWWJAIL" | echo "In script before : $WWWJAIL" | ||
WWWJAIL=/apache.jail | WWWJAIL=/apache.jail | ||
− | echo "In script after : $WWWJAIL"</ | + | echo "In script after : $WWWJAIL"</syntaxhighlight> |
Close and save the file. Run it as follows: | Close and save the file. Run it as follows: | ||
− | < | + | <syntaxhighlight lang="bash" >chmod +x /tmp/dottest.sh</syntaxhighlight> |
Now, define a variable called WWWJAIL at a shell prompt: | Now, define a variable called WWWJAIL at a shell prompt: | ||
− | < | + | <syntaxhighlight lang="bash" >WWWJAIL=/foobar |
− | echo $WWWJAIL</ | + | echo $WWWJAIL</syntaxhighlight> |
Sample outputs: | Sample outputs: | ||
<pre>/foobar</pre> | <pre>/foobar</pre> | ||
Run the script: | Run the script: | ||
− | < | + | <syntaxhighlight lang="bash" >/tmp/dottest.sh</syntaxhighlight> |
Check the value of WWWJAIL: | Check the value of WWWJAIL: | ||
− | < | + | <syntaxhighlight lang="bash" >echo $WWWJAIL</syntaxhighlight> |
You should see the orignal value of $WWWJAIL (/foobar) as the shell script was executed in a subshell. Now, try the dot command: | You should see the orignal value of $WWWJAIL (/foobar) as the shell script was executed in a subshell. Now, try the dot command: | ||
− | < | + | <syntaxhighlight lang="bash" > . /tmp/dottest.sh |
− | echo $WWWJAIL</ | + | echo $WWWJAIL</syntaxhighlight> |
Sample outputs: | Sample outputs: | ||
<pre>/apache.jail</pre> | <pre>/apache.jail</pre> | ||
Line 54: | Line 53: | ||
|previous=Use the trap statement to catch signals and handle errors | |previous=Use the trap statement to catch signals and handle errors | ||
|next=Compound command}} | |next=Compound command}} | ||
+ | <meta name="keywords" content="subshell, bash, linux, linux subshell, bash subshell, unix subshell"></meta> | ||
+ | <meta name="description" content="Explains a subshell which is nothing but a child process launched by a shell or shell scripts under UNIX / Linux bash shell."></meta> |
Latest revision as of 22:50, 29 March 2016
← Use the trap statement to catch signals and handle errors • Home • Compound command →
- Whenever you run a shell script, it creates a new process called subshell and your script will get executed using a subshell.
- A Subshell can be used to do parallel processing.
- If you start another shell on top of your current shell, it can be referred to as a subshell. Type the following command to see subshell value:
echo $BASH_SUBSHELL
OR
echo "Current shell: $BASH_SUBSHELL"; ( echo "Running du in subshell: $BASH_SUBSHELL" ;cd /tmp; du 2>/tmp/error 1>/tmp/output)
- Any commands enclosed within parentheses are run in a subshell.
Exporting Functions and Variables
A subshell does not inherit a variable's setting. Use the export command to export variables and functions to subshell:
WWWJAIL=/apache.jail
export WWWJAIL
die() { echo "$@"; exit 2; }
export -f die
# now call script that will access die() and $WWWJAIL
/etc/nixcraft/setupjail -d cyberciti.com
- However, environment variables (such as $HOME, $MAIL etc) are passed to subshell.
Use exec command to avoid subshell
You can use the exec command to avoid subshell. The exec command replaces this shell with the specified program without swapping a new subshell or proces. For example,
exec command
# redirect the shells stderr to null
exec 2>/dev/null
The . (dot) Command and Subshell
The . (dot) command is used to run shell scripts as follows:
. script.sh
The dot command allows you to modify current shell variables. For example, create a shell script as follows called /tmp/dottest.sh:
#!/bin/bash
echo "In script before : $WWWJAIL"
WWWJAIL=/apache.jail
echo "In script after : $WWWJAIL"
Close and save the file. Run it as follows:
chmod +x /tmp/dottest.sh
Now, define a variable called WWWJAIL at a shell prompt:
WWWJAIL=/foobar
echo $WWWJAIL
Sample outputs:
/foobar
Run the script:
/tmp/dottest.sh
Check the value of WWWJAIL:
echo $WWWJAIL
You should see the orignal value of $WWWJAIL (/foobar) as the shell script was executed in a subshell. Now, try the dot command:
. /tmp/dottest.sh
echo $WWWJAIL
Sample outputs:
/apache.jail
The value of $WWWJAIL (/apache.jail) was changed as the script was run in the current shell using the dot command.
← Use the trap statement to catch signals and handle errors • Home • Compound command → <meta name="keywords" content="subshell, bash, linux, linux subshell, bash subshell, unix subshell"></meta> <meta name="description" content="Explains a subshell which is nothing but a child process launched by a shell or shell scripts under UNIX / Linux bash shell."></meta>