This is a collection of hints and tips for solving real-world problems with shell scripts, mixed with examples of how to use various features of the shell, particularly the more complex Bash syntax - along with a few funny items too. You can return to the main tutorial for the main "how to" of shell scripting, or browse the articles below for individual tasks, problem areas, gotchas, and tips.
This section provides some worked examples of shell scripting code, inspired by real-world scenarios.
Each entry is marked with a "pepper rating" as shown in this table:
![]() | Funny | Just a joke / meme /etc |
![]() | Easy | A beginner should be able to understand most or all of this |
![]() | Medium | Developing, moving your skills up a level |
![]() | Hard | Pushing the boundaries |
Why is a newline at the end of a text file important to many Unix/Linux tools?
Why do tools like Vim and Git warn me about missing newlines?
What kind of effect can it have on a shell script or other tools which process such a file?
1970, The UNIX Epoch and the British Three Year Summer - Why the UNIX Epoch (midnight on 1st January 1970) is actually 1am in UK Time
CRLF (DOS) and LF (*nix) text file formats.
With a footnote about '/bin/bash^M: bad interpreter'
An interview with Brian Kernighan, one of the co-creators of Unix, on the story behind the GREP search tool
Parsing short command-line arguments (-v, -f filename, etc)
When non-technical people see me running nmap (or strace, or lsof, or anything else...)
In which we use a completely unsuitable programming language for fun and learning purposes
The problem with using shell functions to return integers
An apparently contradictory position - how do you make a shell script abort on errors, but be able to check for errors like "grep foo /etc/hosts" and act accordingly?
Using Bash's pattern matchin to implement the well-known strstr() function.
This function uses Bash's simple built-in integer mathematical capabilities to display a duration (in seconds) in a more human-readable form
Using GNU Date to work out how many days until a given event (such as Christmas, 25th December)
A useful way of expanding lists automatically. Useful for Bart Simpson, useful for you.
A useful way of automatically expanding lists of numbers and characters. With Bash, you don't need to type "for x in 1 2 3 4 5", or do lots of other laborious typing. It can expand all sorts of numbers, and letters, for you, in ways that you probably never knew.
Normally you only get the exit status of the last command in a pipeline - this way, you can get the status of any command in a pipline of any length
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 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 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. |