Cookie Consent by Free Privacy Policy Generator Update cookies preferences The Shell Scripting Tutorial - The Shell Scripting Tutorial

The Shell Scripting Tutorial

Written by , Senior DevOps Engineer with 30+ years of Unix, Linux and automation experience, and author of Shell Scripting: Expert Recipes for Linux, Bash and more.


4 July 2000 First published
v4.6 Current version
29 June 2026 Last updated
29 June 2026 Last reviewed

Purpose Of This Tutorial

This tutorial is written to help people understand shell script programming (aka shell scripting), 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 latest version of this tutorial is always available at https://www.shellscript.sh. You can check the verison information at the top-right of this page. This ensures you are reading the most up-to-date version from the authoriative source.

A Brief History of sh

Steve Bourne wrote the original Bourne shell, which appeared in the Seventh Edition Bell Labs Research version of Unix. Since then, many variants and successors have appeared, including csh, ksh, zsh, and others. Bash (the "Bourne Again" Shell) is one of the most widely used shells today. It extends the original Bourne shell by adding many additional features while maintaining compatibility with the core shell scripting concepts established by Bourne. This tutorial restricts itself to Bourne-compatible shell scripting, providing a baseline that works across many Unix and Linux environments. It covers the fundamentals of shell scripting, while the Shell Scripting Examples section includes additional examples showing how Bash provides useful functionality beyond the standard Bourne shell.

Audience

This tutorial assumes some prior experience; namely:

  • Use of an interactive Unix/Linux shell
  • Minimal programming knowledge - use of variables, functions, is useful background knowledge
  • Understanding of some Unix/Linux commands, and competence in using some of the more common ones. (ls, cp, echo, etc)
  • Programmers of ruby, 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.

You may want to review some of the feedback that this tutorial has received to see how useful you might find it.

Typographical Conventions Used in This Tutorial

Code segments and script output will be displayed as monospaced 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 shown with a like this, and include a reference to the plain text of the script, where available like this: my-script.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. So it is likely that you will need to change the permissions on your script, to make it executable. If your script is named "myscript.sh" then you will need to change its permissions, like this:
$ chmod u+rx myscript.sh
$ ./myscript.sh

 Next: Philosophy   

My Paperbacks and eBooks

My Shell Scripting books, available in Paperback and eBook formats. This tutorial is more of a general introduction to Shell Scripting, the longer Shell Scripting: Expert Recipes for Linux, Bash and more book covers every aspect of Bash in detail.

Shell Scripting Tutorial

Shell Scripting Tutorial
is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and in paperback format good to keep by your desk as an ever-present companion.

Also available in PDF form from Gumroad:Get this tutorial as a PDF
Shell Scripting: Expert Recipes for Linux, Bash and more

Shell Scripting: Expert Recipes for Linux, Bash and more
is my 564-page book on Shell Scripting. The first half covers all of the features of the shell in every detail; the second half has real-world shell scripts, organised by topic, along with detailed discussion of each script.