Changes

Jump to navigation Jump to search
110 bytes added ,  22:49, 29 March 2016
m
Text replacement - "<source lang="bash">" to "<syntaxhighlight lang="bash" >"
Line 6: 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:
<source lang="bash">echo $BASH_SUBSHELL</source>
+
<syntaxhighlight lang="bash" >echo $BASH_SUBSHELL</source>
 
OR
 
OR
<source lang="bash">echo "Current shell: $BASH_SUBSHELL"; ( echo "Running du in subshell: $BASH_SUBSHELL" ;cd /tmp; du 2>/tmp/error 1>/tmp/output)</source>
+
<syntaxhighlight lang="bash" >echo "Current shell: $BASH_SUBSHELL"; ( echo "Running du in subshell: $BASH_SUBSHELL" ;cd /tmp; du 2>/tmp/error 1>/tmp/output)</source>
 
* 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:
<source lang="bash">WWWJAIL=/apache.jail
+
<syntaxhighlight lang="bash" >WWWJAIL=/apache.jail
 
export WWWJAIL
 
export WWWJAIL
 
die() { echo "$@"; exit 2; }  
 
die() { echo "$@"; exit 2; }  
Line 21: Line 21:  
==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,
<source lang="bash">exec command
+
<syntaxhighlight lang="bash" >exec command
 
# redirect the shells stderr to null
 
# redirect the shells stderr to null
 
exec 2>/dev/null</source>
 
exec 2>/dev/null</source>
 
==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:
<source lang="bash">. script.sh</source>
+
<syntaxhighlight lang="bash" >. script.sh</source>
 
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:
<source lang="bash">#!/bin/bash
+
<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"</source>
 
echo "In script after : $WWWJAIL"</source>
 
Close and save the file. Run it as follows:
 
Close and save the file. Run it as follows:
<source lang="bash">chmod +x /tmp/dottest.sh</source>
+
<syntaxhighlight lang="bash" >chmod +x /tmp/dottest.sh</source>
 
Now, define a variable called WWWJAIL at a shell prompt:
 
Now, define a variable called WWWJAIL at a shell prompt:
<source lang="bash">WWWJAIL=/foobar
+
<syntaxhighlight lang="bash" >WWWJAIL=/foobar
 
echo $WWWJAIL</source>
 
echo $WWWJAIL</source>
 
Sample outputs:
 
Sample outputs:
 
<pre>/foobar</pre>
 
<pre>/foobar</pre>
 
Run the script:
 
Run the script:
<source lang="bash">/tmp/dottest.sh</source>
+
<syntaxhighlight lang="bash" >/tmp/dottest.sh</source>
 
Check the value of WWWJAIL:
 
Check the value of WWWJAIL:
<source lang="bash">echo $WWWJAIL</source>
+
<syntaxhighlight lang="bash" >echo $WWWJAIL</source>
 
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:
<source lang="bash"> . /tmp/dottest.sh
+
<syntaxhighlight lang="bash" > . /tmp/dottest.sh
 
echo $WWWJAIL</source>
 
echo $WWWJAIL</source>
 
Sample outputs:
 
Sample outputs:

Navigation menu