Click Here to buy the full 72-page PDF for only £4.99, $9.99 or €6.99,
or the Kindle eBook (UK here - more countries and details here)

This site hosted by HostGator:
Use code STEVEPARKER25 and get 25% off.

1. Introduction

Purpose Of This Tutorial

This tutorial is written to help people understand some of the basics of shell script programming, and hopefully to introduce some of the possibilities of simple but powerful programming available under the bourne shell. As such, it has been written as a basis for one-on-one or group tutorials and exercises, and as a reference for subsequent use.

Getting The Most Recent Version Of This Tutorial

The most recent version of this tutorial is available from: http://steve-parker.org/sh/sh.shtml. Always check there for the latest copy.

A Brief History of sh

Steve Bourne wrote the Bourne shell which appeared in the Seventh Edition Bell Labs Research version of Unix.
Many other shells have been written; this particular tutorial concentrates on the Bourne and the Bourne Again shells.
Other shells include the Korn Shell (ksh), the C Shell (csh), and variations such as tcsh.
This tutorial does not cover those shells. Maybe a future version will cover ksh; I do not intend to write a tutorial for csh, as csh programming is considered harmful.

Audience

This tutorial assumes some prior experience; namely:
  • Use of an interactive Unix shell
  • Minimal programming knowledge - use of variables, functions, is useful background knowledge
  • Understanding of how Unix commands are structured, and competence in using some of the more common ones.
  • Programmers of perl, python, C, Pascal, or any programming language (even BASIC) who can maybe read shell scripts, but don't feel they understand exactly how they work.

Typographical Conventions Used in This Tutorial

Significant words will be written in italics when mentioned for the first time.

Code segments and script output will be displayed as preformatted text.
Command-line entries will be preceded by the Dollar sign ($). If your prompt is different, enter the command:

PS1="$ " ; export PS1

Then your interactions should match the examples given (such as $ ./my-script.sh below).
Script output (such as "Hello World" below) is displayed at the start of the line.

$ echo '#!/bin/sh' > my-script.sh
$ echo 'echo Hello World' >> my-script.sh
$ chmod 755 my-script.sh
$ ./my-script.sh
Hello World
$
Entire scripts will be surrounded by thick horizontal rules and include a reference where available to the plain text of the script:
first.sh
#!/bin/sh
# This is a comment!
echo Hello World	# This is a comment, too!

Note that to make a file executable, you must set the eXecutable bit, and for a shell script, the Readable bit must also be set:
$ chmod a+rx first.sh
Click Here to buy the full 72-page PDF for only £4.99, $9.99 or €6.99,
or the Kindle eBook (UK here - more countries and details here)

This site hosted by HostGator:
Use code STEVEPARKER25 and get 25% off.
Steve's Bourne / Bash shell scripting tutorial
Share on Twitter Share on Facebook Share on LinkedIn Share on Identi.ca Share on StumbleUpon

Get the Book:

My Shell Scripting Book:
    Shell Scripting, Expert Recipes for Linux, Bash and more
is available online and from all good booksellers.

Buy my 600-page Shell Scripting Book...

From Amazon USA:

Or from other retailers

Facebook:

You can also join a Shell Scripting community on Facebook:

Get the Tutorial as a PDF:

Shell Scripting Tutorial
Buy the 72-page PDF (£4.99 / $9.99 / €6.99) of this tutorial
(Free Sample)

Get the Tutorial on Kindle:

For Kindle from Amazon USA:
For Kindle from Amazon UK:

And you can always Download my Free Shell Scripting Cheatsheet PDF

DefectiveByDesign.org