April 21, 2008

You, bash users, are driving me crazy!

Category: Sysadmin.

This is a visceral message with a lot of emotion (I mean I am not thinking before writing) so I will exceptionally use my big voice:


Details bellow... with some words in capital letters as proposed by [RFC2119].

Four things you SHALL be aware of

  1. You SHOULD NOT write bash scripts, you SHOULD write sh scripts

    bash provides a lot of bullcrap non-posix non-standard extensions. Using them makes your script not portable. Conforming to bourne shell is not that hard. You SHOULD use bash only if it provides a feature you MUST rely on and that cannot be emulated using a few more shell script lines.

  2. You SHALL NOT expect /bin/sh to be bash

    By definition, /bin/sh is the Bourne SHell. Why may it be the Bourne-Again SHell? Some GNU/Linux distros provide bash as the bourne shell since it is [almost] compatible with standard bourne shell. If your script need bash, your shebang SHALL point to bash, not sh.

  3. You SHALL NOT use /bin/bash, you SHALL use /usr/bin/env bash instead

    Another common mistake is to expect bash to be installed in /bin. Some systems will not mess-up the system core applications with the user programs so don't forget you SHALL NOT rely on an absolute path to bash. You SHALL therefore use /usr/bin/env to spawn the bash interpreter.

  4. You SHALL NOT type in blanks in shebangs

    Probably the most exasperating common error: you SHALL NOT put any blank character before the shell path in the shebang line: some versions of the bourne shell will fail to locate the interpreter without this simple precaution.

    #! /usr/bin/env bash
    # ^
    # WRONG!  No space here!

Template for shell scripts REQUIRING bash

Okay, your shell script utterly depends on bash. I propose you the following template as a starter:

#!/usr/bin/env bash
# I an aware that I SHOULD have written bourne shell script instead of this!
# Spank! Spank! Spank! I'm such a naughty programmer!
# Date: date
# Author: name <email@example.com>

Your code goes here

You might also be interested in this additional note for a proper shebang.


On August 4, 2008, Tony Shadwick wrote:

Wow. :) Now if I could convince my fellow perl coders to follow your logic, I would be sooooo much happier.

I have a co-worker that insists on writing his shebang like this (and he's on windows!):


Now - that isn't going to work. :P It just won't. It works with ActivePerl on Windows, because it effectively ignores the shebang. I can't shell script to save my life. I've tried. :( So I wind up requiring perl at every turn, but at least I follow your logic:

#!/usr/bin/env perl

Fact it, 99% of the perl code out there expects perl in /usr/bin, which is just plain FALSE, and in fact the FreeBSD port for perl places a symlink to /usr/bin acknowledging this fact, despite the default port install location being /usr/local/bin (unless overridden).

So...bleh. I'm just glad someone agrees with me. :)

Comments RSS feed | Leave a Reply…