Saturday, August 23, 2014

Better Bash Scripting

Robert Muth:

I start every bash script with the following prolog:

#!/bin/bash
set -o nounset
set -o errexit

This will take care of two very common errors:

  1. Referencing undefined variables (which default to “”)
  2. Ignoring failing commands

[…]

Bash has a number of (underappreciated) ways to manipulate strings.

[…]

Some commands expect filenames as parameters so straightforward pipelining does not work. This is where <() operator comes in handy as it takes a command and transforms it into something which can be used as a filename.

3 Comments RSS · Twitter

Bug: use /usr/bin/env bash since many (most?) systems have /usr/local/bin/bash not in /bin. Only Linux is weird.

@sara Which systems? On Mac OS X, it’s at /bin/bash.

FreeBSD, Solaris (I think), OSX with macports to get a recent version.

Leave a Comment