Bash

Overview

Bash, apart from being a UNIX shell, is also a programming language in it’s own right. It is commonly referred to as a scripting language because the script is not compiled, but rather interpreted at runtime.

Shebang

The recommended shebang to use at the top of a bash script is:

The above variant gives the greatest portability, as different UNIX systems put the bash program at different locations on the system (e.g. OpenBSD and FreeBSD both do not have bash at /bin/bash). The above shebang is an instruction to run the first bash that is found on the PATH.

A less optimal bash shebang is:

This is not as portable as the bash location is hard-coded into the shebang.

Finally, it is not recommended to use the shebang:

if you want to run bash, as /bin/sh is usually a symlink to the operating systems default shell, which may or may not be bash (it could also be dash).

Returning From Scripts

The return statement can be used to return from a function within a script.  return has nothing to do with the exit code.

The exit code of the last command run in a script is used as the exit code that is returned to the parent program.

You can also use the command exit <number> at any time to return from the script.

ANSI Escape Codes

Bash supports various ANSI escape codes. This includes the commonly used codes to colour text printed to the terminal.

Getting The Current Script Directory

You can read the current script directory as a string into a variable using the following code:

Command-line Argument Parsing

Command-line argument parsing in bash is notoriously tricky.

Thankfully, some open-source third party libraries come to the rescue. One of the most powerful command-line argument parsing libraries for shell scripts is shflags, a port of the Google gflags library for the UNIX shell.

The simple Hello, World example (taken from their website) is shown below. If you create a file called hello_world.sh with the following contents:

You could then call this as so:

As shown above, it also supports automatic help! Although the shebang in the above example is #!/bin/sh, this can be changed to #!/usr/bin/env bash without any issues. Please note: If using bash, make sure that set -e is not active when shflags is sourced, otherwise your script will crash when sourcing, with no helpful error message displayed.

Posted: May 13th, 2013 at 11:47 am
Last Updated on: June 2nd, 2017 at 12:53 pm