The Shell Scripting Tutorial

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:

FunnyJust a joke / meme /etc
EasyA beginner should be able to understand most or all of this
MediumDeveloping, moving your skills up a level
HardPushing the boundaries

What does [noeol] mean?

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?

IFS, the Internal File Seperator

I got ChatGPT to explain IFS ... it did a pretty good job!

1970 and the UNIX Epoch

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

Drunk explanation of TCP and HTTP

An alternative answer to a tired interview question

Parsing long-form command-line arguments

Using the --help style of arguments instead of just -h


The relatively obscure 'join' command


Redirecting Standard Output (stdout) and Standard Error (stderr)

1st, 2nd, 3rd

Writing Ordinal Numbers (1st, 2nd, 3rd, etc)


CRLF (DOS) and LF (*nix) text file formats.
With a footnote about '/bin/bash^M: bad interpreter'

Oct 31 == Dec 25

Why is Oct 31 == Dec 25?

Formatting files with ls and stat

Psst! This article isn't really about ls!

Edit a Running Script

What happens when you change a running shell script?

Sex and Drugs and Unix

From the 1983 Usenix conference


A standardised banner function for better presentation


An interview with Brian Kernighan, one of the co-creators of Unix, on the story behind the GREP search tool

#! - The Shebang

or HashPling, or whatever you want to call it.

Tux checks Linux licenses

Excuse me Sir, just checking your Linux licenses...

Thou Shalt Not Kill -9

Thou Shalt Not Kill -9

Parsing short-form command-line arguments

Parsing short command-line arguments (-v, -f filename, etc)

Why are people scared of screens of text?

When non-technical people see me running nmap (or strace, or lsof, or anything else...)

Changing to Uppercase or Lowercase

Changing the case of text in variables

echo -n or \c

Newline in Echo: When to use -n, when to use \c


Creating a simple Spinner to show a script is still running

Temporary File Names

Generating temporary (and possibly unique, and/or identifiable) filenames

Again, Again!

Doing a different thing to the same thing (or, Teletubbies mode, if you prefer!)


In which we use a completely unsuitable programming language for fun and learning purposes

Witty 'man' page entries

Wit and Humour in *nix Documentation


A 'sleep' substitute which gives regular updates

Controlling Loops

Controlling and Quitting Loops in Shell Scripts

Return codes, Functions, and the number 255

The problem with using shell functions to return integers

Poetry in Code

The Waka Waka Poem - onomatopoeic poetry!

Failing on Errors, whilst also Checking for Errors

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?

Pattern Substitution

Editing variable values in-place

Sorting Files by Name

Including advanced sorts, and coping with spaces

Editing a line in a text editor

Copy a command into a text editor for further editing

Efficiently Pinging a Subnet

Two methods to use the ping command to quickly scan a network

The strstr() function in Bash

Using Bash's pattern matchin to implement the well-known strstr() function.

Giving 'root' access to Developers

What could possibly go wrong?!

Search and Replace with a Variable

Editing Contents of Variables

Calculating Hours, Minutes and Seconds

This function uses Bash's simple built-in integer mathematical capabilities to display a duration (in seconds) in a more human-readable form


Brian Kernighan, one of the co-creators of Unix, on the Unix Pipeline

Software Development Models

Explained with Cars

The O'Reilly Animals

An Adult Colouring Book

rm -rf

A Confession


Using GNU Date to work out how many days until a given event (such as Christmas, 25th December)

The Story of Mel

A Real Programmer

chmod 777

Not Needed.

Linux Administrator

The old "What I Do / What I Think I Do" meme.

Progress Bar

Creating a simple Progress Bar for a shell script.

Fork Bomb!

Using shell builtin commands to deal with a fork-bombed machine.

Sorting on Fields

Understanding the 'sort' utility

Simple Shell Functions

A vital step on the journey towards shell scripting proficiency.

Expanding Lists

A useful way of expanding lists automatically. Useful for Bart Simpson, useful for you.

Expanding Sequences

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.

Checking the exit status of ANY command in a pipeline

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

Nifty use of Grep in a Loop

That you wouldn't ordinarily think of

RPM Package Versions

And how to deal with awkward version numbers

Trap Die Trap

The smart way to deal with errors

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.