Code SVG logo for fpahs.net Cursive initials "fp" in a lavender circle Snippets

Shell Scripting

I write a fair amount of shell scripts on macOS at my day job, mostly in Dash, the Debian Almquist shell.

Why not Bash?

macOS 15 Sequoia still ships with Bash version 3.2, which is years behind the current 5.x release. This is not an issue in itself, because Bash has a lot of nice features, including:

However, Apple will eventually remove Bash in favor of Dash and Zsh. I cynically expect this to happen in a point release, much like the removal of Python 2.7 from macOS 12.3 Monterey.

Why not Zsh then?

Zsh certainly has all the niceties of Bash. I'd like to think that I stick to POSIX compliant shell because of its portability and speed. However, most of my shell scripts involve macOS specific tools like scutil(8), so the portability argument is weak. Armin Briegel even has a good book titled Moving to Zsh that a coworker recommended to me.

Error Messages

This error message function appears in all my Dash scripts:

          
  error() {

    local err
    readonly err="${1}"

    # LINENO is best used alongside xtrace, i.e.,
    # 'set -x' or 'set -o xtrace' when debugging.

    echo "${LINENO}: ${err}" >&2
  }
        

I steal liberally from Jan Schaumann, Armin Briegel, the Google Shell Style Guide & a variety of good books.