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 rating:
|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|
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
Books and eBooks
You can mail me with this form. If you expect a reply, please ensure that the address you specify is valid. Don't forget to include the simple addition question at the end of the form, to prove that you are a real person!