|
January
12, 2004 -
Subshells & Subshell Grouping - Part
II
|
The
Korn shell automatically creates a
subshell to perform these operations:
|
·
commands enclosed by parenthesis
· each section of a pipeline (minus the
last)
· background processes
· co-processes
· command substitution
|
Enclosing
commands in parenthesis - ( command1;
command2; commandn; ) - produces a
different result than enclosing the same
commands in braces - { command1; command2;
commandn; }. The reason for this is
because commands inside of braces are
executed within the current session (a
subshell is not created).
Consider the following series of commands:
|
[livefire@hawk] # pwd
/export/home/livefire
[livefire@hawk] # ( cd /tmp; pwd; )
/tmp
[livefire@hawk] # pwd
/export/home/livefire
[livefire@hawk] # { cd /tmp; pwd; }
/tmp
[livefire@hawk] # pwd
/tmp
[livefire@hawk] #
|
As
you can see from the above output, when the
cd command is executed from within
parenthesis, the working directory of the
shell remains unchanged. The same is
obviously not true when braces are used.
|
|
|
|
|
|
Learn
more...
If you are new to the UNIX or Linux
operating system and would like to learn
more, you may want to consider
registering for LiveFire Labs' UNIX
and Linux Operating System Fundamentals
online training course.
If you already have a solid grasp of the
fundamentals but would like to learn more
about the Korn shell and basic and
advanced shell scripting, taking our Korn
Shell Scripting course will be
beneficial to you.
Our
innovative hands-on training model allows
you to learn
UNIX by completing hands-on
exercises on real servers in our Internet
Lab.
More
Tips...
· Popular
UNIX Tips from the Past
|
|
|
|
|
|
|
Receive
the UNIX Tip, Trick, or Shell Script of the
Week by Email
|
|
|