Introduction To The UNIX Shell: by Mark Virtue and The Virtual Training Company
Introduction To The UNIX Shell: by Mark Virtue and The Virtual Training Company
Introduction To The UNIX Shell: by Mark Virtue and The Virtual Training Company
by Mark Virtue
and The Virtual Training Company
Chapter 1
Introduction
About this Course
This course contains instruction on the following topics:
The UNIX shell (specifically the Bourne family of shells)
Common text-based (command-line) UNIX programs, such as
ls
vi
grep
etc
UNIX GUI (graphical user interface, such as X-
Windows) usage will not be taught
The commands taught in this course are compatible
with all brands of UNIX (including Linux)
Audience and Prerequisites
You might want to learn about the UNIX shell if:
You are using a version of UNIX at work/home/school (e.g.
Linux) and want to know more about the commands available
You have a need to log into a computer remotely across the
Internet and run commands there (e.g. your ISP’s computer)
You need to learn the basics of the shell so that you can then
learn about shell script programming for the purposes of UNIX
system administration or CGI programming
No prior knowledge of UNIX or UNIX shell commands is
assumed
Familiarity with computers in general and command-line
interfaces (such as DOS) in particular will be helpful,
but is not necessary
Chapter 2
Understanding UNIX
What is UNIX?
UNIX is the name given to a family of operating systems
An operating system is a software platform upon which
programs may be run
Windows 2000 is an example of a (non-UNIX) operating system
Each operating system is usually tied to only one type of
computer (such as Intel x86 or Sun SPARC)
There are many flavours (or variants) of UNIX produced
by a variety of technology companies
Many are incompatible with each other, in the sense
that they cannot run each others’ programs
In order to fully understand what UNIX is – and how the
different versions are related to each other – it is
necessary to know something of UNIX’s history
UNIX History
UNIX was originally developed in the early 1970s by
AT&T’s Bell Laboratories
It was so useful an operating system that other
organisations (including universities) expressed interest
in developing versions of their own, and were given the
source code for free
Soon all the large computer vendors were marketing
their own (diverging) versions of UNIX optimised for
their own computer architectures, boasting many
different strengths and features (including Microsoft’s
own effort: Xenix)
UNIX History (cont.)
It quickly became apparent that, although UNIX
systems were available everywhere, they seldom were
able to interoperate without significant effort. The
trademark UNIX was ubiquitous, but it was applied to a
multitude of different, incompatible products
In 1987, the two leading vendors of UNIX – AT&T
(System V) and Sun Microsystems (BSD) combined
their efforts to produce System V Release 4 (SVR4),
foisting what they hoped was a new standard upon the
UNIX world
UNIX History (cont.)
This only served to further divide the industry, and many
other market players banded together to develop their
own open UNIX variant, called OSF/1 (Open Software
Foundation UNIX version 1)
To introduce a sense of unity, an organisation called
X/Open began putting in place a set of open standards
that would allow greater interoperability between UNIXs
In 1993, AT&T sold their UNIX business to Novell, who
sold it to X/Open (now The Open Group)
Linus Torvalds ported a version of UNIX to the PC and
gave the source code to the community at large – Linux
was born
Which UNIX?
There are currently hundreds of UNIX variants running
on computers around the world
There are roughly 80 still being developed and
supported
The following table details some of the major variants
Which UNIX? (cont.)
UNIX Variant Company
AIX IBM
A/UX Apple
FreeBSD (free)
HP-UX Hewlett-Packard
IRIX Silicon Graphics
Linux (free, PC) Various
NEXT Next
SCO-UNIX (PC) Santa-Cruz Organisation
Solaris Sun
Ultrix Digital
UnixWare (PC) Novell (now SCO)
QNX (real-time) Quantum Software
Which UNIX? (cont.)
How compatible are these variants of UNIX?
Specifically, if a person learns one UNIX, will those
skills by useful when running another UNIX?
The answer is complex:
For end users, the UNIX shell (the subject of this course) is
virtually identical across all UNIX’s
Some UNIX’s offer GUIs (X-Windows, Motif, etc). These can
differ widely, but having learned one, most end users will find
the others relatively easy to learn
For advanced users, UNIX shell-scripting is virtually identical
across all UNIX’s
UNIX system administrators will find significant differences
when administering a variety of UNIX systems
UNIX Architecture
UNIX is a terminal-based operating system, meaning:
Many users may be simultaneously using the one computer,
each with their own keyboard and monitor (and sometimes
mouse) – known as a terminal (compare this with PCs, which
have only a single keyboard and monitor for each computer)
The programs that each user is running are all competing for
the computers CPU time and memory. This means that one
user can potentially slow down the system for the rest of the
users on the system
Some UNIX terminals are capable of displaying a GUI,
complete with icons and mouse support. Such
terminals are known as X-terminals
UNIX is also capable of existing within a network
Five Computers on a Network
PC or
Workstation
One Computer with Five Terminals
Terminal or
X-terminal
Chapter 3
Shell
Operating
System
What is the UNIX Shell? (cont.)
The UNIX shell is a text-based, command-line-driven program
Each line of text that the user types is interpreted by the program
(the shell) as one command (program) to run
It looks like this:
Welcome to UNIX
Have a nice day!
$ ls
Documents
Readme.txt
output.file
$ _
Which Shell?
There are a variety of (similar) UNIX shells to choose
from
The original and most widely supported shell is called
the Bourne shell (after S.R. Bourne). These days it is
considered the most basic of shells. Its program
filename is sh
There are a number of Bourne shell derivatives, each
offering a variety of extra features, including:
The Korn shell (after David Korn) (ksh) (not open/free)
zsh
and many more
Which Shell? (cont.)
It is possible to write scripts using the Bourne shell as
an interpreter. Such scripts are known as shell scripts
The facilities that the Bourne shell makes available for this
purpose allow the shell to be thought of as a programming
language (a scripting language, to be precise)
Compare these with DOS batch files, interpreted by
command.com
All Bourne-compatible shells share the same programming
language (that’s why they’re called Bourne-compatible)
Much UNIX system administration and Web back-end
processing (CGI) is done via Bourne shell scripts
The Virtual Training Company offers a training course on UNIX
Shell Script Programming (Bourne-compatible shells)
Which Shell? (cont.)
In an effort to offer more power to shell programmers,
another shell was developed in which the programming
language was more closely related to the powerful “C”
programming language. This shell was called the
C-shell (filename csh)
This in turn spawned its own family of compatible
shells, most notably tcsh
The Bourne and C-shell families are completely
incompatible from a programming standpoint, and they
differ in their implementations of other certain features
(such as command history), but otherwise they can be
used interchangeably
Logging In
Virtually all UNIX systems require users to log in
(identify themselves to the computer with a username
and password) before any programs can be run
On some systems, as soon as a user has successfully
logged in, a shell is automatically started for them to
use to run programs
Note: at the end of this course we will look at how to choose
which shell is automatically started for you
Your username and password will have been created
for you by the system administrator
One username (root) has full system privileges,
meaning they are never denied access to a resource
Logging In (cont.)
On other systems (notably those with GUIs), once the
user has logged in they have the option of starting any
number of (concurrent) command-line shells by clicking
the appropriate icon
Basic Commands
In this module we will simply type in a few basic
commands to get the feel for text-based commands
For each of the following, type the name of the
command, and then press the ENTER key (on some
UNIX systems this key is labelled RETURN or )
Command Purpose
uname Details about the machine you are logged into (including the
version of UNIX). Try also: uname -a
ps Find out which shell you’re running
cal Display a calendar for the current month
Try also: cal 2001 / cal 10 2001 / cal 9 1752
passwd Change your login password (follow the prompts and be careful)
Command Syntax
The syntax (structure) of every command entered into
the UNIX shell is as follows:
$ prog-name [options] [arguments]
Where:
prog-name is the name of the program you wish to run
(for example, ls)
options are single letters prefixed by a “–” (dash, minus sign) that
modify the behaviour of the program (for example, -a). These are
always optional, meaning that the program will still do something if no
options are specified
arguments are any other words (separated by spaces or tabs) that
the program needs to perform the task you wish it to perform (such as
a file or directory name). Some programs require a certain number of
arguments, others (like ls) do not require any
Command Syntax (cont.)
Notes:
The program name must be first. It is not possible to type in the
name of a document and expect the shell to know how to open
it (the UNIX shell has no concept of file “associations”). The
program name must be the name of a binary program or an
executable (text) script. Nothing else will run
Case is significant. In other words, ls is different from LS
It is possible to specify the location of the program as well
(for example: /bin/ls ./myscript)
There must be a space between every element of the
command-line. Commands like ls-la or cd/ will not run
The options may be specified in a variety of ways. All of the
following are synonymous:
ls -lax ls -l -x -a ls –ax -l
Getting Help
Help text is available for every standard UNIX program
This help is known as the “Manual Pages”, or man
pages
These are a reference tool, not a “how-to” guide
They are used as follows: man prog-name
For example: man cal
It is difficult to determine which program is best for a
certain task. A program that offers partial help is
whatis
Many versions of the UNIX man pages can be found on
the Internet
Logging Out
There are two ways of exiting a (Bourne-compatible)
shell:
Typing the exit command
Typing Ctrl-d
(in UNIX command-line programs, Ctrl-d always means:
“I have finished typing – I will be typing nothing further into this
program”)
Once the shell exits, UNIX automatically logs you out,
and you are (typically) presented with a login screen
again
If you are using a GUI, exiting the shell will simply close
the shell’s window – the user will remain logged in
Chapter 4
reports proposals
Security
Users and Groups
UNIX is a "multi-user" operating system
This means:
More than one user may interact with (log on to) the system at
any given moment
Each user has a separate set of access privileges for system
resources (such as files)
Combining Programs
- Pipes and Filters
Standard Output
Most running UNIX programs produce output
Such output usually ends up on the screen (the user's monitor)
This output can be "redirected" to one of two other
"places":
1. A file
2. Another program
To redirect output to a file, the ">" symbol is used
For example:
ls –l > listing
A file called listing is created in the current directory
that contains the output of the ls program
Standard Output (cont.)
To redirect output to another program, the "|" ("pipe")
symbol is used
For example:
who | wc
When this command is typed on the command line, the
shell does the following:
Starts the who program
Starts the wc program
Connects the two in such a way that the output of the first
program is "piped" to the second, where it is used as input
who wc
Standard Input
The previous diagram implies (correctly) that programs
can take input
Not many programs take input
By default, a program's input comes from the keyboard
An appropriate use of the shell can cause a program to
take input from one of two other places:
1. A file
2. Another program
Input is read from a file by using the "<" symbol
For example:
wc < file
Standard Input and Output
The input and output described above are known as
Standard Input and Standard Output
The diagram below summarises this:
Keyboard Screen
File File
Running UNIX
Program program Program
Standard Output
Standard Input
Standard Input and Output (cont.)
Why would we want to do this? Why would we want to
connect programs together, or read data from files?
Most of the command-line utilities that come with UNIX
adhere to the philosophy that "complex" tasks may be
performed by combining simple programs
This "roll-your-own" approach has made UNIX very
successful, and is the foundation of shell-scripting
About Filters
A filter is a UNIX command-line utility that has the
following properties:
1. It takes standard input
2. It performs some processing on the data it reads
3. It produces output based upon that input
For example, wc is a filter. The processing it performs
is counting lines, words and characters. ls, however,
is not a filter (it takes no input)
Filters are used to process the data produced by other
programs and the data in files
Common Filters
The following programs are filters that are used
regularly in UNIX:
Filter Processing done to Standard Input
cat None
more Pagination
grep Removal of lines that do not contain certain text
sort Sorting
wc Counting of lines, words and/or characters
tee Duplication – write to files and screen
sed Basic editing
awk Anything
Searching for Text in Files
In the previous table, it was said that grep's processing
is "removal of lines that do not contain certain text"
This can be put more meaningfully in two other ways:
grep is used to search for text in files (or Standard Input)
grep is a true filter, in every sense of the word
grep has the following usage:
grep pattern filename(s)
For example:
grep Mark *.txt
grep uses its own set of wildcards
Use grep with find to locate files in all directories
Standard Error
Each program actually produces two sets of output:
Keyboard Screen
File File
Running UNIX
Program program Program
Screen
File
Standard Error
Standard Error (cont.)
To redirect Standard Error to a file, use "2>"
(Standard Output can also be redirected using "1>")
This means that a command's output can be redirected
to two separate files, if need be, as follows:
command1 > fileA 2> fileB
It is possible to redirect all output to the same place, as
follows:
command1 > fileA 2>&1
Any form of output may be redirected to /dev/null if it
is not wanted at all
Chapter 7
Process Control
About Processes
A process is a running program
Every process is assigned a unique process ID (usually
in the range 1-30000)
Running processes for the current login session can be
examined by using the ps command
ps also has the following options:
-u userDisplay all processes for the given user
-f or -l Display listings with more details
-e Display all processes on the system
About Processes (cont.)
It is sometimes useful to think of a process stack:
ps
sh
vi
sh
Running Commands Asynchronously
If a process is run asynchronously ("in the background"),
the shell does not wait for the process to finish before
presenting another prompt and allowing you to start
further processes
To run a process asynchronously, append a "&" to the
command line
Notes:
Commands that take Standard Input should never be run in the
background
Commands that produce Standard Output (or Error) can be run
in the background, but it is a good idea to redirect their output to
a file (or /dev/null)
Asynchronous processes are terminated when you log out
Killing Processes
Any running process may be stopped by using the
kill program, as follows:
kill process-ID
For example:
kill 12548
Notes:
Some processes can leave the system in an unexpected state
when they are killed – try to avoid using kill if possible
kill will only succeed if you have permission to kill a process
Some processes are programmed to ignore attempts to kill
them. If you really want the program to end, and you've already
tried kill, then try:
kill -9 process-ID
Jobs
If you are using ksh or bash, it is possible to suspend
(or stop) running processes, in order that further
processes may be started. A stopped process is called
a job
While a process is running, use Ctrl-z to stop it
The command fg will resume the most recently
stopped program
The command jobs is used to see a list of all stopped
jobs. Each is prefixed by a number that can be used by
fg to resume that particular process
Note that programs running asynchronously cannot be
stopped
More Process Control
All running processes are assigned a priority by the
operating system. This priority determines what share
of the CPU time the process is given
It is possible to adjust this priority using the nice
command
A higher priority will result in the process completing
sooner, at the expense of other processes. A lower
priority will prevent your process from impinging too
much upon the speed of other processes
Priorities are numbers in the range -20 (highest) to 19
(lowest). The default priority is 10
More Process Control (cont.)
nice is used as follows:
nice -priority command [arguments]
For example:
nice --20 find / -name output.file
or
nice -19 backup /home
Note that on most UNIX's, no user except root is
permitted to increase a process's priority
More Process Control (cont.)
Recall that processes running asynchronously are
terminated when the user logs out
It is possible to prevent this, by using the nohup
command (short for "no hang-up"), as follows:
nohup command [arguments] &
For example
nohup backup /home &
Any Standard Output the process produces will be
written to a file called nohup.out, unless it is
redirected elsewhere
Scheduling Commands
It is possible to schedule commands to be run at a
particular time in the future (usually within the next 24
hours)
The command at is used to schedule commands, as
follows:
$ at 2315
> cd /home/fred
> tar cf /dev/fd0 *
> rm –rf *
> Ctrl-d
$
Any Standard Output produced is emailed to the user
Chapter 8
ESC
Shell
a, A, i, I, o, O, s, S, c, C, etc
Manipulating Files
Like most text editors and word processors, vi can be
started with a document to edit, or without – as an
"empty canvass", as follows:
To use vi to edit a file:
vi filename(s)
For example:
vi script1
vi *.txt
Or to start vi with no file:
vi
Manipulating Files (cont.)
Once vi has been started, you will find yourself in
Command Mode
To perform some simple editing:
Use (for example) a to "append" text after the current cursor
position (and enter Insert Mode)
Enter some text, for example:
The quick brown fox
Jumps over the Lazy Dog
Press ESC to return to Command Mode
We will now look at the many ways in which we may
quit vi and save the changes
Manipulating Files (cont.)
:w write (save) the file (only if a name has been specified)
:w file write to the specified file (save as)
:q quit (only if no changes have been made)
:wq save and then quit
:x or ZZ save the file (if changes have been made), then quit
:q! abandon any changes and quit
:w! write to a read-only file (that you own)
:e file open the specified file (if no changes have been made)
:e! file open the specified file (abandon any changes)
:e# or ^6 open the last file edited
:n and :n! open the next file specified on the command line
:rew rewind to the first file specified on the command line
:f or ^g display current file details
Moving Around
PC keyboard special keys (arrow keys, Page Up, End,
etc) sometimes work in vi
Any command listed below with a can be prefixed
with a number n to move n intervals
Moving on the current line
SPACE or l Move ahead one character
BACKSPACE or h Move back one character
$ Move to the last character on the line
^ or 0 Move to the first character on the line
fX Move to (find) the next instance of character X
; Move to the next instance of character X
Moving Around (cont.)
Moving between lines
ENTER or j or + Move to the next line
k or - Move to the previous line
^f Move forward one page (page down)
^b Move back one page (page up)
^d Move down half a page
^u Move up half a page
G Go to last line in file
1G Go to first line in file
nG Go to line n in file
Moving Around (cont.)
Other move commands
/pattern Move to the next occurrence of pattern
n (N) Move to the next (previous) occurrence
w (b) Move forward (back) one word
% Find the matching bracket: ( ) [ ] and { }
]] ([[) Move to the next (previous) C function
mx Mark a line with label x
'x Go to line labelled x
Basic Editing
The following commands take you into Insert Mode,
where text is then entered. Press ESC to return to
Command Mode:
a (i) Append (insert) text after (before) the current cursor
A (I) Append text to the end (beginning) of the line
o (O) Start (open) a new line after (before) the current line
s Substitute the current character with text
cw Change the remainder of the word to new text
Basic Editing (cont.)
The following commands are also used for basic
editing, but do not take you to Insert Mode:
x (X) Delete (cut) the char under (before) the cursor
dd Delete (cut) the current line
p Put (paste) the recently deleted text
yy Yank (copy) the current line
rx Replace the current character with x
The following commands special commands are very
useful:
u Undo the last command
U Restore the current line to how it was when
you arrived on it
. Repeat the last command
Advanced Editing
The following commands are simply useful:
>> (<<) Shift the current line to the right (left)
J Join the next line to the current line
^l Redraw the screen
:m,ns/abc/xyz/g
Substitute all occurrences of abc with xyz on
lines m to n (using grep-like regular expressions)
(use 1,$ for all lines in file, . for current line)
:!command Run a shell command (e.g. ls)
(use % for current filename, # for alternate file)
:sh Obtain a temporary shell
Configuring vi
I-node Table
How Files are Stored (cont.)
I-node
Blocks
Table
Understanding Links
In UNIX, unlike some other operating systems, it is
possible for a file to have more than one name, in more
than one directory
Each name is called a link (including the original name)
All links to a file share the same i-node, meaning that
every link shares the same permissions, etc
No link is considered "more important" than any other
link. In particular, if there is more than one link, there is
no concept of an "original" and "duplicates"
Understanding Links (cont.)
The rm program simply "unlinks" the link that you specify
When all links to a file have been unlinked, the data that
the file contained is "deleted" (marked as available)
ls -l can be used to examine how many links any
given file has (including the one in the listing)
There is no simple program that can show all links to a
given file. Instead, use find and search for an i-node
number, as follows:
find dir –inum 12345
Understanding Links (cont.)
91282 91284
Dies machine 1%6e@-_ 7[;z.
is nicht fur $9_&!)_.ywefr
gefingerpoken _data%8@ &
und mitten-g 9(@&..-'<b$sW
raben by das 6^2@-';]{ .fR
dumkopfen. Is _gfunc(*2@,.U
91283 91285
Hello. My na #!/bin/sh
me is Fred. I #
work in the m # This shell
arketing depa #
rtment. I am trap 1 15 ""
32 years old, usage()
Blocks
Linking Files
The program ln is used to create links to existing files
Its usage is identical to that of cp, except the file is not
copied (duplicated), another reference (link) to the file is
simply created
Symbolic Links
All the links mentioned so far are also known as hard links,
to distinguish them from symbolic links
To create a symbolic link to a file means to create a small
file (a separate file) that contains nothing but a reference to
the original's filename
This means that, when referring to symbolic links, the terms
"original" and "link to original" make sense
Microsoft Windows also offers a form of symbolic link,
known as a shortcut (Windows does not support hard links)
ln -s is used to create symbolic links
Using ls -l, symbolic links are indicated by a "l" file type
Symbolic Links (cont.)
91282 91284
Dies machine 1%6e@-_ 7[;z.
is nicht fur $9_&!)_.ywefr
gefingerpoken _data%8@ &
und mitten-g 9(@&..-'<b$sW
raben by das 6^2@-';]{ .fR
dumkopfen. Is _gfunc(*2@,.U
91283 91285
Hello. My na #!/bin/sh
me is Fred. I #
work in the m # This shell
arketing depa #
rtment. I am trap 1 15 ""
32 years old, usage()
Blocks
UNIX File Types
Most files in UNIX represent actual files (in the regular
sense). ls -l represents these with a "-"
The table below shows the other types of file that exist:
ls -l Type Description
d directory Container for other files
l symbolic link Reference to another file
c character device Represents character-based
hardware (e.g. serial port)
b block device Represents block-based hardware
(e.g. floppy disk)
p pipe (FIFO) Communications file
Mounting
Microsoft DOS and Windows use designators like a:,
c: and k: to represent each accessible filesystem
In UNIX, all files that users can access are found in a
single directory tree (with its root called "/")
This is achieved by mounting each available filesystem
onto a subdirectory of the "root" filesystem, including:
Other hard disks
Floppy disks
Network disks
Each filesystem is typically mounted onto an empty
subdirectory
Mounting (cont.)
tom sue
documents email
source library
Mounting (cont.)
It is easy to determine used and available disk space on
any filesystem, by using the df (disk free) command
The disk space used by any directory can by displayed
with the du (disk usage) command
Chapter 10
Communication
Using telnet
telnet is a program used to obtain a login prompt
(and/or a shell) on another UNIX machine
The remote machine must be connected to the local
machine by a TCP/IP network (including the Internet)
In order to log on to a remote system, you will need:
A telnet program (such programs exist for many platforms,
including Windows)
The name or IP address of the remote machine (for example,
shell.abc.com or 123.234.12.99)
(usually) A username and password (your local username and
password will not necessarily work on the remote machine)
Using mail