Sh - the Bourne Shell
Last Updated: Wed Aug 17 12:38:31 EDT 2011
Check out my other tutorials on the
Unix Page
, and my
blog
Thanks to suggestions/correectons from:
Ryan Penn
Helmut Neujahr
DJ Phasik
Table of Contents
Bourne Shell, and filename expansion
Shell Concepts
Verifying which shell you are running
Shell basics
Meta-characters and Filename expansion
Finding the executable
Quoting with the Bourne Shell
Nested quotations
Strong versus weak quoting
Quoting over several lines
Mixing quotation marks
Quotes within quotes - take two
Placing variables within strings
Variables
A subtle point
The set command
Environment Variables
Special Environment Variables
PATH - Sets searchpath
HOME - Your home directory
CDPATH - cd searchpath
IFS - Internal Field Seperator
PS1 - Normal Prompt
PS2 - Secondary Prompt
MAIL - Incoming mail
MAILCHECK - How often to check for mail
SHACCT - Accounting file
MAILPATH - searchpath for mail folders
Bourne Shell Variables - Alternate Formats
Using quoting and shell variables
Using curly braces with variables
${variable?value} - Complain if undefined
${variable-default} - Use default if undefined
${variable+value} - Change if defined
Bourne Shell Tutorial
http://www.grymoire.com/Unix/Sh.html
1 of 66
11/21/2011 12:03 PM
${variable=value} - Redefine if undefined
Undefining Variables
${x:-y}, ${x:=y}, ${x:?y}, ${x:+y} forms
Order of evaluation
Special Variables in the Bourne Shell
Positional Parameters $1, $2, ..., $9
$0 - Scriptname
$* - All positional parameters
$@ - All positional parameters with spaces
$# - Number of parameters
$$ - Current process ID
$! - ID of Background job
$? - error status
$- Set variables
Options and debugging
Special options
X - Bourne Shell echo flag
V - Bourne Shell verbose flag
Combining flags
U - unset variables
N - Bourne Shell non-execute flag
E - Bourne Shell exit flag
T - Bourne Shell test one command flag
A - Bourne Shell mark for export flag
K - Bourne Shell keyword flag
H - Bourne Shell hash functions flag
The $- variable
- - Bourne Shell hyphen option
Other options
C - Bourne Shell command option
S - Bourne Shell shell-session option
I - Bourne Shell shell-interactive option
R - Bourne Shell restricted shell option
P - Bourne Shell privileged shell option
unset
Bourne Shell: Status, Pipes and branches
Unnecessary process execution
$@ versus ${1+$@}
Status and Wasted Processes
Simple Flow Control
Changing Precedence
Putting it all together
Bourne Shell Flow Control Commands: If, While and Until
Commands that must be first on the line
While - loop while true
Until - loop until true
Bourne Shell Flow Control Commands
For - Repeating while changing a variable
Case - Checking multiple cases
Break and continue
Expr - Bourne Shell Expression Evaluator
Bourne Shell Tutorial
http://www.grymoire.com/Unix/Sh.html
2 of 66
11/21/2011 12:03 PM
Arithmetic Operators
Relational Operators
Boolean Operators
The string operator
Precedence of the Operators
Berkeley Extensions
Bourne Shell -- Functions and argument checking
Passing values by name
Exiting from a function
Checking the number of arguments
UNIX conventions for command line arguments
Checking for optional arguments
Job Control
Copyright 2001, 2005 Bruce Barnett and General Electric Company
All rights reserved
You are allowed to print copies of this tutorial for your personal use, and link to this page, but you are not
allowed to make electronic copies, or redistribute this tutorial in any form without permission.
How to build your own complex commands from the simple commands in the UNIX toolbox.
This tutorial discusses of Bourne shell programming, describing features of the original Bourne
Shell. The newer POSIX shells have more features. I first wrote this tutorial before the POSIX
shells were standardized. So the information describe here should work in POSIX shells as it is a
subset of the POSIX specifications.
You're not getting the most out of UNIX if you can't write shell programs!
Bourne Shell, and filename expansion
This sections covers the Bourne shell. The manual pages are only 10 pages long, so it shouldn't be difficult to
learn, right? Well, apparently I'm wrong, because most of the people I know learned one shell to customize
their environment, and stayed with the C shell ever since. I understand the situation. It's hard enough to learn
one shell language, and after struggling with one shell for a while, they are hesitant to learn another shell.
After a few scripts, the new user decides the C shell is "good enough for now" and it ends right there. They
never take the next step, and learn the Bourne shell. Well, perhaps this chapter will help.
The Bourne shell is considered the primary shell in scripts. All UNIX systems have it, first of all. Second, the
shell is small and fast. It doesn't have the interactive features of the C shell, but so what? Interactive features
aren't much use in scripts. There are also some features the Bourne shell has that the C shell doesn't have. All
in all, many consider the Bourne shell to be the best shell for writing portable UNIX scripts.
Shell Concepts
What is a shell, anyway? It's simple, really. The UNIX operating system is a complex collection of files and
programs. UNIX does not require any single method or interface. Many different techniques can be used. The
oldest interface, which sits between the user and the software, is the shell. Twenty five years ago many users
didn't even have a video terminal. Some only had a noisy, large, slow hard-copy terminal. The shell was the
interface to the operating system. Shell, layer, interface, these words all describe the same concept. By
Bourne Shell Tutorial
http://www.grymoire.com/Unix/Sh.html
3 of 66
11/21/2011 12:03 PM