Writing Bash scripts is fundamental to automate and understand many tasks on common Unix-like environments. If you are new to Bash (or even Unix-like systems) you'll have it a lot easier if you take a weekend to learn some fundamental principles before starting to script your stuff. No matter how experienced you are in programming, everything you'll find regarding Bash is much easier to follow and understand if you know some things before.
High quality online readings
IMHO, the following resources are good starting points:
- Linux Shell Scripting Tutorial (LSST) – Extremely good resource, especially for beginners.
- Bash FAQ – Many answers to frequently asked questions.
- The official GNU bash manual – Reference work. If you are generally familiar with Bash/*ix or simply search for a specific thing: look at the manual. Nearly everything should be documented there.
Things you should understand before you write serious scripts
To make it much easier for yourself, make sure you understand1) the following (the resources mentioned above will help you):
- Streams and redirection
Standard Input (stdin, 0), Standard Output (stdout, 1) and Standard Error (stderr, 2). It is really fundamental to understand the stream concept. How streams are working. And how to manipulate, redirect and merge them. This is particularly important before writing much code or libraries. Otherwise you may have to change many things over and over again to make your scripts are interacting smoothly with the system (→ error logging etc.).
Example of things you should be able to do blindfolded:
foobar's stderr (2) into
foobar's stdout (1):
- If you redirect
foobar's stdout (1) into a file now, all error messages will also be written into the file because stderr (2) is merged into stdout (1):
foobar > ~./logfile.log 2>&1
- If you understand the two steps above, it is clear what the following does:
echo "message" 1>&2
echoprints something to stdout (1).
1>&2merges stdout into stderr. Therefore ”
message” will be printed as error to stderr(2) instead of creating simple standard output – this is the way to go if you want to print a simple error message. You also need redirections if you want to use sudo.
- Exit codes
This is particularly important before writing much code to make sure your scripts are interacting smoothly with other applications.
- Pipes and Filters
- Subshells and the so called Subshell Problematic
- Background processes
E.g. how to start them (→
- Globbing and the Internal Field Separator (IFS)
Make sure you be able to change and restore the IFS. And know how to use
findand why you need it.
- Regular Expressions and useful tools
findcommands are available on nearly every system and are very helpful when you need to edit, find, manipulate or create files and data.
- Learn how to detect if you are root
if [ "$(id -u)" != "0" ] then echo "This script needs root privileges!" 1>&2 exit 1 fi
- Learn how to define functions
- Here Documents
See the Here Documents at the LSST for details.
Last but not least: find a project
You'll learn the most if your scripts are doing real jobs. Create a backup script. Or try to configure your whole desktop by script to make things comfortable on freshly installed systems.