Shell Advanced Features
Shell Substitution Capabilities
There are three types of substitution in the shell:
Variable substitution
Command substitution
Tilde substitution
2
Shell Variable Storage
Local Variables
color=blue
count=3
dir_name=/home/user3/tree
Program Code
/usr/bin/sh
PS1=$
PATH=/usr/bin:/usr/contrib/bin:/usr/local/bin
Environment HOME=/home/user3
Variables TERM=70094a
SHELL=/usr/bin/sh
3
Syntax:
Setting Shell Variables
name=value
Examples: $ color=lavender Assign local variable.
$ count=3 Assign local variable.
$ dir_name=tree/[Link]/ford Assign local variable.
$ PSl=hi_there$ Update environmental variable.
hi_there$set Display all variables and values.
color=lavender
count=3
dir_name=tree/[Link]/ford
/usr/bin/sh
PATH=/usr/bin:/usr/contrib/bin:/usr/local/bin
HOME=/home/user3
SHELL=/usr/bin/sh
.
.
.
4
Variable Substitution
Syntax:
$name Directs the shell to perform variable substitution
Example:
$ echo $PATH
/usr/bin:/usr/contrib/bin:/usr/local/bin
$ PATH=$PATH:$HOME:.
$ echo $PATH
/usr/bin:/usr/contrib/bin:/usr/local/bin:/home/user3:.
$ echo $HOME
/home/user3
$ file_name=$HOME/file1
$ more $file_name
<contents of /home/user3/file1>
5
Variable Substitution (Continued)
$ dir_name=tree/[Link]/ford
$ echo $dir_name
tree/[Link]/ford
$ ls -F $dir_name
sedan/ sports/
$ my_ls="ls -aFC"
$ $my_ls
./ file.1 tree/
../ file.2
$ $my_ls $dir_name
./ ../ sedan/ sports/
$ cd /tmp
$ dir_name=/home/user2/tree/[Link]/retriever
$ $my_ls $dir_name
./ ../ golden labrador mixed
6
Command Substitution
Syntax:
$(command)
Example:
$ pwd
/home/user2
$ curdir=$(pwd)
$ echo $curdir
/home/user2
$ cd /tmp
$ pwd
/tmp
$ cd $curdir
$ pwd
/home/user2
7
Tilde Substitution
$ echo $HOME
/home/user3
$ echo ~
/home/user3
$ cd tree
$ echo $PWD
/home/user3/tree
$ ls ~+/[Link]
collie poodle retriever shepherd
$ echo $OLDPWD
/home/user3/mail
$ ls ~-
/home/user3/mail/[Link] /home/user3/mail/[Link]
$ echo ~tricia/file1
/home/tricia/file1
8
Displaying Variable Values
$ echo $HOME
/home/user3
$ env
HOME=/home/user3
PATH=/usr/bin:/usr/contrib/bin:/usr/local/bin
SHELL=/usr/bin/sh
$ set
HOME=/home/user3
PATH=/usr/bin:/usr/contrib/bin:/usr/local/bin
SHELL=/usr/bin/sh
color=lavender
count=3
dir_name=/home/user3/tree
$ unset dir_name
9
Transferring Local Variables to the
Environment
Syntax:
export variable Transfer variable to environment
c
o
l
or
=
l
av
e
n
de
r
c
o
u
nt
=
3
/
u
s
r/
b
i
n/
s
h
P
S
1
P
A
T
H
H
O
M
E
S
H
E
LL $
e
n
v
c
o
u
nt
=
3
c
o
l
or
=
l
av
e
n
de
r $
e
x
po
r
t
10
Passing Variables to an Application
$ vi
local variables
local variables
color=lavender parent sleeps $
color=lavender /usr/bin/sh
/usr/bin/sh env var
TERM=98550
env var
TERM=98550
local variables
local variables /usr/bin/vi
color=lavender
env var
/usr/bin/sh TERM=98550
env var
TERM=98550
STEP 2: exec: program and local data
STEP 1: fork: program space are replaced with program
and data spaces and data of requested program
are duplicated (/usr/bin/vi) and program is
executed. When program
completes, control returns to the
parent (/usr/bin/sh) and the
prompt is displayed.
11
Monitoring Processes
$ ps -f
UID PID PPID C STIME TTY TIME COMMAND
user3 4702 1 1 08:46:40 ttyp4 0:00 -sh
user3 4895 4702 18 09:55:10 ttyp4 0:00 ps -f
$ ksh
$ ps -f
UID PID PPID C STIME TTY TIME COMMAND
user3 4702 1 0 08:46:40 ttyp4 0:00 -sh
user3 4896 4702 1 09:57:20 ttyp4 0:00 ksh
user3 4898 4896 18 09:57:26 ttyp4 0:00 ps -f
$ exec ps -f
UID PID PPID C STIME TTY TIME COMMAND
user3 4702 1 0 08:46:40 ttyp4 0:00 -sh
user3 4896 4702 18 09:57:26 ttyp4 0:00 ps -f
$
12
Child Processes and the Environment
export color=lavender
$ ksh (create child shell process)
$ ps -f
UID PID PPID C STIME TTY TIME COMMAND
user3 4702 1 0 08:46:40 ttyp4 0:00 -sh
user3 4896 4702 1 09:57:20 ttyp4 0:00 ksh
user3 4898 4896 18 09:57:26 ttyp4 0:00 ps -f
$ echo $color
lavender
$ color=red
$ echo $color
red
$ exit (exit child shell)
$ ps -f (back in parent shell)
UID PID PPID C STIME TTY TIME COMMAND
user3 4702 1 0 08:46:40 ttyp4 0:00 -sh
user3 4895 4702 1 09:58:20 ttyp4 0:00 ps -f
$ echo $color
lavender
13