"Taking a LiveFire Labs' course is an excellent way to learn
Linux/Unix. The lessons are well thought out, the material is
explained thoroughly, and you get to perform exercises on a real
Linux/Unix box. It was money well spent."
Ray S.
Pembrook Pines, Florida
LiveFire Labs' UNIX and Linux Operating System Fundamentals
course was very enjoyable. Although I regularly used UNIX systems
for 16 years, I haven't done so since 2000. This course was a
great refresher. The exercises were fun and helped me gain a real
feel for working with UNIX/Linux OS. Thanks very much!"
Ming Sabourin
Senior Technical Writer
Nuance Communications, Inc.
Montréal, Canada
Read more student testimonials...
Receive UNIX Tips, Tricks, and Shell Scripts by Email
LiveFire Labs' UNIX Tip,
Trick, or Shell Script of the Week
Subshells & Subshell Grouping - Part I
Invoking a shell script by entering only the script's name on
the command line creates a subshell in which the script runs. It is
important to understand that the subshell has a separate environment
from the login/parent shell. Alternatively, prefacing the script name
with a "." (dot) and a space causes the script to run within the
context of the login session, as if you had typed each line of the
script in at the command prompt. A simple shell script named
display_info will be used to demonstrate this concept:
[livefire@hawk] # cat display_info
print
ps -f
print
print "The value of x is: $x"
print
[livefire@hawk] #
Prior to running display_info as a subshell, a value will be assigned
to the variable x, and the current session's process information will
be displayed.
[livefire@hawk] # x=50
[livefire@hawk] # print $x
50
[livefire@hawk] # ps -f
UID PID PPID C STIME TTY TIME CMD
livefire 455 453 0 13:27:20 pts/2 0:00 -ksh
[livefire@hawk] #
[livefire@hawk] # display_info
UID PID PPID C STIME TTY TIME CMD
livefire 540 455 0 15:11:09 pts/2 0:00 -ksh
livefire 455 453 0 13:27:20 pts/2 0:00 -ksh
The value of x is:
[livefire@hawk] #
Notice that a child process running another instance of ksh (the Korn
shell program) is created, and the value assigned to the local
variable x in the parent shell is not visible in the subshell. This
time the same script will be executed without creating a subshell:
[livefire@hawk] # . display_info
UID PID PPID C STIME TTY TIME CMD
livefire 455 453 0 13:27:20 pts/2 0:00 -ksh
The value of x is: 50
[livefire@hawk] #
As you can see from the output, a second process is not created and
the previously defined value for x is displayed.
Next week we'll discuss when the Korn shell creates and does not
create subshells, and why or why not you may want to use subshells.
Read the NEXT article in this series -
Subshells &
Subshell Grouping - Part II