Shell Scripting Tips

Shell Scripting Tips 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 article is marked with a rating, either for funny, or with peppers, with one being easy, two peppers a bit harder, and three peppers being the more advanced topics.

Hints and Tips

Drunk Explanation of TCP and HTTP

An alternative answer to a tired interview question. It is such a ridiculous question that it deserves to be ridiculed.

Join

The relatively obscure 'join' command

Getopts

Parsing long-form command-line arguments

Your code after too many edits

You know it's true!

Redirection

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

1st, 2nd, 3rd

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

dos2unix

dos2unix

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

Oct 31 == Dec 25

Oct 31 == Dec 25

Why is Oct 31 == Dec 25?

Formatting files with <code>ls</code> and <code>stat</code></b>

Formatting files with ls and stat

Psst! This article isn't really about ls

Edit a Running Script

Edit a Running Scrip

What happens when you change a running shell script?

Sex & Drugs & Unix™

Sex & Drugs & Unix™

From the 1983 Usenix conference

banner

banner

A standardised banner function for better presentation

Grep

Pipelines

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

#! - The Shebang

#! - The Shebang

or HashPling, or whatever you want to call it.

Tux checks Linux licenses

Tux checks Linux licenses

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

Thou Shalt Not Kill -9

Thou Shalt Not Kill -9

Thou Shalt Not Kill -9

Parsing short-form command-line arguments

Parsing short-form command-line arguments

Parsing short-form command-line arguments

Why are people scared of screens of text?

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 to Uppercase or Lowercase

Changing the case of text in variables

echo -n or \c

echo -n or \c

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

Spinner

Spinner

Creating a simple Spinner to show a script is still running

Temporary File Names

Temporary File Names

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

Again, Again!

Again, Again!

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

Wordsearch

Wordsearch

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

Witty 'man' page entries

Witty 'man' page entries

Wit and Humour in *nix Documentation

Sleepy

Sleepy

A 'sleep' substitute which gives regular updates

Controlling Loops

Controlling Loops

Controlling and Quitting Loops in Shell Scripts

Return codes, Functions, and the number 255

Return codes, Functions, and the number 255

The problem with using shell functions to return integers

Poetry in Code

Poetry in Code

The Waka Waka Poem - onomatopoeic poetry!

Failing on Errors, whilst also Checking for Errors

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

Pattern Substitution

Editing variable values in-place

Sorting Files by Name

Sorting Files by Name

Including advanced sorts, and coping with spaces

Editing a line in a text editor

Editing a line in a text editor

Copy a command into a text editor for further editing

Efficiently Pinging a Subnet

Efficiently Pinging a Subnet

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

The strstr() function in bash

The strstr() function in bash

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

Giving Developers Root Access

Giving Developers Root Access

What Could Possibly Go Wrong?!

Search and Replace within a Variable

Search and Replace within a Variable

Editing Contents of Variables

Calculating Hours, Minutes and Seconds

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

Pipelines

Pipelines

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

Search and Replace within a Variable

Search and Replace within a Variable

Editing Contents of Variables

Software Development models

Software Development models

Explained with Cars

The O'Reilly Animals

The O'Reilly Animals

An Adult Coloring Book.

Where There Is A Shell, There Is A Way

Where There Is A Shell, There Is A Way

There Just Is.

Christmas

Christmas

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

Shift

Shift

A quirk of the "shift" command explained. You probably did not know this.

The Story Of Mel

The Story Of Mel

A Real Programmer.

Linux Administrator

Linux Administrator

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

Progress Bar

Progress Bar

Creating a simple Progress Bar for a shell script.

Fork Bomb!

Fork Bomb!

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

Sorting on Fields

Sorting on Fields

Understanding the 'sort' utility.

Simple Shell Functions

Simple Shell Functions

A vital step on the journey towards shell scripting proficiency.

Expanding Lists

Expanding Lists

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

Expanding Sequences

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

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

Nifty use of Grep in a Loop

That you wouldn't ordinarily think of

RPM Package Versions

RPM Package Versions

And how to deal with awkward version numbers

Trap Die Trap

Trap Die Trap

The smart way to deal with errors

Key To The Icons

Each article is rated by an icon; the table below shows what each icon means.

Icon Label Description
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

Shell Scripting Tips

Shell Scripting Tutorial