Introduction to UNIX Operating
Systems
Chapter 9
The UNIX Shell
Tariq Nuur, Professor
Chapter Objectives
• Grasp an in-depth understanding of UNIX
shell system, features and capabilities
• Grasp the concept of variables, definition
and usage in preparation for shell scripting
• Use additional metacharacters and
commands
• Modify and execute startup files, processes
and process management.
The UNIX Shell
• Remember the UNIX consists of:
The SHELL is the heart of the UNIX system and
stays in memory from the time you start a
session until the session ends.
Utilities are UNIX external commands
(programs) that you invoke and system has to
search for it before any execution can take
place.
The UNIX Shell Continued…
• The UNIX shell is a program that loads into
memory when you log on to the system.
• The shell is ready when the system prompt
appears.
• When you type a command the shell finds
and starts the program.
• The shell is a C program and it is stored in
the /bin directory.
The UNIX Shell
•There are different types of shells:
•Bourne (sh)
•Korn (ksh)
•Cshell (csh)
•Bourne Again Shell (bash)
•Tcsh
We will focus on the most used command of bash
Shell.
Bash Shell
• The Korn shell is an enhancement of
Bourne which was derived from the person
who developed the shell.
• Although the shell starts the moment you log
into your system you can invoke another
shell by tying bash at the system prompt.
• Type: bash and press Enter.
• Type: exit and press [Enter] to exit the shell.
Features of the shell…
• Command Execution: Anything typed at
the system prompt is interpreted by the
shell.
• Filename Substitution (Filename
Generation): the shell performs the
substitution and then executes the program.
The program does not perform the
substitution.
• I/O Redirection: Input/output redirection is
performed by the shell.
More Shell Features…
• Pipes (pipelines): enable you to connect simple
programs together to perform a complex task.
• Environment Control: enables you to customize
your environment to suit your needs.
• Background processing: enables you to run
processes in the background while running
processes in the foreground.
• Shell scripts: a sequence of shell commands
stored in a file. The file is later used to execute
the stored programs.
The echo Command
• The echo command is used to display
messages on your screen.
• Type: echo Hello there and press Enter.
• The argument string can be any
number of characters long. However,
strings containing metacharacters must
be enclosed in quotation marks.
Your Turn…
• Display a simple message on the screen:
Type: echo Hi, this is a test. [Enter]
• Display the message in two line:
Type: echo –e Hi, “\n” this is a test. [Enter]
• Save the output in a file
Type: echo –e Hi, “\n” this is a test > test1
[Enter]
• View the file you created from output
Type: more test1 [Enter]
Are we having fun yet? Try
this…
• Do not produce the next line at the end of the
message:
• Type: echo -e Hi, “\n” this is a test. “\c” [Enter]
• What happens to the blanks
• Type: echo This is a test. [Enter]
• What happens to the blanks now?
• Type: echo “This is a test.” [Enter]
Removing The Meaning of
Metacharacters
• Sometimes metacharacters have to be
overridden in a technique called quoting or
escaping. The set of quoting characters is
as follows:
• backslash \
• double quotation mark “ ”
• or single quotation mark ‘ ‘
Escaping Metacharacters
• The backslash \ is used to cause the
character that follows it to be interpreted as
an ordinary alphanumeric character.
• Example to remove all files beginning with
temp you would type, rm temp?
• To remove a file name with a metacharacter
as part of the name, you type rm temp\?
Displaying the Metacharacters
• To display all the metacharacters type the
following:
• echo \< \: \” \’ \$ \? \& \/ \\ [Enter]
• The double quotation marks are used to
display the meaning of most special
characters.
• Type, echo > [Enter]
• Type, echo “>” [Enter]
More Examples…
•Type, ls –C [Enter]
•Type, echo * [Enter]
•Type, echo “*” [Enter]
•Type, echo “\”The UNIX System”\” [Enter]
As you can see the backslash is necessary
before a double quote if the quotes are to
appear around the title.
The single quote is also used to
escape special characters…
• Type, echo ‘< > “ $ ? & | ‘
Therefore, the single quote is used for
special character except any special
character surrounded by single quotes will
not be interpreted by the shell as a special
character.
Variables
• Variables are unique names that serves as
memory location for a running program of
which its value changes during the run
cycle.
• Since the shell program handles the user
interface and interprets commands, in order
for the shell to service all your requests it
has to keep track of your information by
storing it.
• The information is stored in shell variables.
Variables Continue
• Variables are items you set to specific
values to control or customize your
environment. There are two types of
variables: environment and local.
Environment Variables
• Environment variables are called standard
variables; they have names that are known
to the system.
• Environment variables are defined by the
system administrator.
• For example the variable TERM is assigned
to your terminal type: TERM=vt100
Local Variables
• Local variables are defined by you the user.
They are assigned to the system and
programs by you.
• You type the word “set” at the command
prompt to display shell variables.
• Type, set [Enter] to display the shell
variables.
• The variable names are on the left of the
equal “=“ sign and the value assigned to
each variable is on the right of the = sign.
Assigning Variables
• When you assign a variable you must use
the exact name when referring to the
variable.
• You use the unset command to remove a
variable name by typing:
unset <variable-name>
Example: To unset the XYZ variable you
would type unset XYZ.
Assigning Values to Variables
• You create variables by typing:
<variable_name>=<value>
Example: To create the variable age and
assign the value of 25 to age type:
age=25
The shell treats every value assigned to a
variable as a string of characters.
Assigning Values to Variables
(Cont’d)
• If a string contains white spaces or tabs you
must enclose the entire string in double
quotes.
• Example:
message=“Have a nice day.”
Displaying the value of a
variable
• You can use echo to display the value of a
variable.
• The value must be preceded by the $ when
used with echo to display the value of the
variable.
• Your turn…
Your turn…
• Create the age variable by typing:
age=25 [Enter]
• Type, echo Hi, nice day [Enter]
• Type, echo age [Enter]
• Type , echo $age [Enter]
• Type, echo You are $age years old [Enter]
• Type, echo $age “$age” ‘$age’ [Enter]
Understanding the Standard
Shell Variables
• HOME
• IFS
• MAIL
• MAILCHECK
• PATH
• PS1
• PS2
• CDPATH
• SHELL
• TERM
• TZ
More Metacharacters
• Grave Accent Mark: The single back quotation
` ` marks are used to execute the enclosed
command and to insert the commands output at
the same point on the command line. It is also
used for command substitution.
`command`
Example: echo The date and time is: `date`
will display the value of the date command including
the string “The date and time is:”
Using the Semicolon to Sequence
Commands
• You may enter a series of commands on the
command line followed by a semicolon and
the shell will execute each from left to right.
• Type, date; pwd; ls –C [Enter]
• You may also use a sequence of commands
to create files.
• Type, ls –C > list; date > today; pwd [Enter]
Using Parentheses to Group
Commands
• You may also group commands using
parentheses.
• Type, (ls –C; date; pwd) > outfile [Enter]
• Type more outfile or
• Cat outfile to list the contents of the outfile.
Background Processing Using
The Ampersand
• Remember, UNIX is a multitasking system
that enables you to execute several
programs concurrently.
• The shell metacharacter ampersand &
enables you to run programs in the
background as long as they do not require
keyboard input.
• Normally large programs or processes are
executed in the background.
Example…
• To sort data and store the results in a file called
sorted you would type:
sort data > sorted & [Enter]
• To create background processes with multiple PID
numbers, place & after each instruction. Example
date & pwd & ls –C &
• To run the date command in the foreground while
the large sorted file is being processed you would
just type the date command.
• To bring a background process forward you would
type jobs to get the process number and type fg
(job number) or %number.
Chaining Commands Using the
Pipe Operator
• You can use the pipe | operator to run a
combination of commands.
• The pipe operator enables you to director
the standard output of one command into
another command for processing.
• Example to send the output of ls to the
printer you would type, ls –l | lp [Enter]
• Type, ls –C | wc –l [Enter] to count the
words characters and lines of the ls –C
command output.
Timing a Delay with Sleep
Command
• The sleep command causes the process
executing it to go to sleep for a specified
number of seconds. For example
sleep 10; echo “I am awake!”
Displaying the PID:
The ps Command
• The ps (process status) command is used to obtain
the status of the active processes in the system.
The information is arranged in four columns:
– PID: The process ID number
– TTY: your terminal number that controls the process.
– TIME: time duration (in seconds) that your process is
running.
– COMMAND: the name of the command being
executed.
• ps options:
– [-a that displays the status of all the active processes.]
– [-f that displays full information that includes
command line.]
Keep On Running:
The nohup Command
• The nohup command causes your
background processes to be immune to
terminating signals after you have logged
off. Example:
nohup (sleep 120; echo “job done”) &
Terminating a Process:
The kill Command
• The kill command is used to terminate any
unwanted process. In order to use the kill
command, you must have the PID of the process
that you intend to terminate.
– Example kill 22515 (PID)
• Use -9 signal for a sure kill, e.g.,
– kill -9 22515
• The PID 0 (zero) causes all processes associated
with your shell to be terminated that includes your
login shell. Example
– Kill -9 0
Splitting the Output:
The tee command
• The tee command provides the user the
flexibility to view an output on the screen
and save it to a file with less time of typing.
• The tee command is usually used with the
pipe (|) operator. For example:
ls –C | tee [Link]
File Searching: The grep
Command
• The grep command is used to search for
specified patterns in a file or list of files.
• The pattern searched by grep is called a
regular expression.
• The grep command is a file searching and
selection command.
Examples
• To search for the word “the” in the myfirst
file type:
grep the myfirst [Enter]
You can also search the file using a pipeline:
Type, more myfirst | grep the [Enter]
Grep Operator Command Options
-c Displays only the count of the matching lines
in each file that contains the match.
-i Ignores the distinction between lowercase
and uppercase letters in the search pattern.
-l Displays the names of the files with one or
more matching lines, not the lines
themselves.
-n Displays a line number before each output
line.
-v Displays only those lines that do not match
the pattern.
The Sort Command
• The sort command is used to sort the
contents of a file containing a list in
alphabetic or numeric order.
• By default the output of a sort is displayed
on the screen. However, the output of a sort
can be redirected to a file or the printer.
Example: to sort a file listing students type,
sort student1 [Enter]
The sort Command Options
Option Operation
-b Ignore leading blanks
-d Use dictionary order for sorting and ignore
punctuation and controls.
-f Ignore lowercase/uppercase distinction.
-n Numbers are sorted by their arithmetic values.
-o Stores the output in the specified file.
-r Reverses the order of the sort from ascending to
descending order.
Example of Sort
• sort –r student1 > [Link]
• sort –r student1 | tee [Link]
Startup Files
• The system profile file is stored in /etc/profile,
which is the first thing the UNIX system executes.
The profile is maintained by system administrator
that typically contains set up for system wide
environment variables.
• The user .profile file is the startup file in the user
home directory that sets up user’s unique
environment.
More on Shell Variables
• export command: makes a list of shells available to
subshells.
• ENV: This is set to absolute pathname of the environment
file at startup.
• HISTSIZE: The variable is set to the number of commands
you intend to keep.
• TMOUT: The variable is set to the number of seconds you
want the system to wait before timing out if you do not
type a command.
• VISUAL: Allows the user to set the command editing for
the user.
The Shell Options
• noclobber: The noclobber option prevents
you from overwriting an existing file if you
redirect output from a command. E.g.
– $set –o noclobber [Enter]
• Ignoreeof: The ignoreeof option prevents
you from accidentally logging yourself off
with [Ctrl-d]
– $set –o ignoreeof [Enter]
Setting Command line Editing
Option
• set –o vi
• EDITOR=/usr/bin/vi
• VISUAL=/usr/bin/vi
More Unix Command
• alias: Used to assign names of frequently used commands
or changing the command names to something that is
easier to remember. E.g.
– $alias del=rm [Enter]
• history: used to track the command history of a user.
– $history [Enter]
• r(edo) Used to repeat the last or previously issued
command.
– $r [Enter] repeats last command issued
– $r vi [Enter] Repeats vi from the history list.
– $r 102 [Enter] repeats command number 102 from the history list.
• fc command provides the capability to list, edit, and re-
execute commands that were previously entered and were
saved in the command history.
– $fc –l [Enter]
Adding Event to Startup
• The shell reads the .profile file in your
HOME directory when you log in. It
executes the commands you want to run at
login time, and initializes the variables that
will be in effect for your login session.
With an editor such as vi, you can edit
the .profile file to include such commands
as date, who, calendar etc. or any other
executable files at startup.
Adding Event Number to the Prompt
• Sometimes, it is useful to know the event number
that the shell gives to each command you enter.
You can change your prompt to include such
information e.g. PS1=“! $” of which the
exclamation mark (!) will tell the system to read
the last event number from your history file, add
one to it, and display it.
• This can be added to your hidden shell file such as
.kshrc to appear each time you log into the system.
Formatting the Linux Prompt
Variable (bash)
• In addition to displaying static character strings in
the prompts, bash provides a list of predefined
special characters that can be used in formatting
the prompt, e.g.
• PS1=“[\!]$ ” and then [Enter] to display the
command number.
• PS1=“[\d]$ ” and then [Enter] to display the
system date between brackets.
• PS1=“[\!][\t]$ ” and then [Enter] to display the
command number and system current time.
Special Character Code Format
Character Meaning
\! Displays the history number of current
command.
\$ Displays a $ in the prompt unless the user
directory is the root. When the user is in the
root, it displays a #.
\d Displays the current date.
\s Displays the name of the shell that is running.
\t Displays the current time.
Additional Linux/UNIX Command
• whatis – This is a brief summary of a command.
e.g. whatis cal
• Use redirection operator to create empty file e.g. >
accountfile
• The touch command can be used to create an
empty file as well e.g. touch [-options] filename
• awk – This is a pattern-scanning and processing
command. This is similar to cat and more
command.
• uniq – removes duplicate lines from one file and
results to another. Ideal in manipulating database
files e.g. uniq [-options] [file1 > file2]
Create a file called parts with the
following data
mufler
mulfler
shocks
alternator
battery
battery
radiator
radiator
coil
spark plugs
spark plugs
coil
Type
$ uniq parts > inventory
Using other command
• Similar to unique, the comm command
identifies duplicate lines. However, it does
not delete duplicate lines.
e.g. comm [-option] file1 file2
• The diff command shows lines that differ
between two files and is commonly used to
determine the minimal set of changes
needed to convert file1 to file2.
e.g. diff [-options] file1 file2
UNIX Process Management
Hands-on Exercise…