Skip to content

Instantly share code, notes, and snippets.

@janxkoci
Created September 11, 2025 11:46
Show Gist options
  • Select an option

  • Save janxkoci/36b3a7897375b11360ede2ab22367c86 to your computer and use it in GitHub Desktop.

Select an option

Save janxkoci/36b3a7897375b11360ede2ab22367c86 to your computer and use it in GitHub Desktop.
bash strict mode template
#!/bin/bash
# bash strict mode
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
@janxkoci
Copy link
Author

janxkoci commented Sep 11, 2025

bash strict mode

The idea is taken from a blog by Aaron Maxwell, however I've seen at least the first part in other sources (e.g. Vince Buffalo's excellent book Bioinformatics Data Skills).

The bash strict mode is meant for scripting, rather than interactive use of bash at the command line. It makes certain silent failures impossible, thus leading to more robust bash scripts.

quick explanation

See the original blog post for more details, but here is the gist:

  • set -e = any error stops the entire script (because, by default, a bash script will keep going even after errors occur)
  • set -u = accessing unset variables throws error (these are most commonly misspelled variables, that can easily lead to disasters)
  • set -o pipefail = errors within pipelines don't abort the pipeline, even with set -e, so this option handles the case
  • IFS=$'\n\t' = bash will do "word splitting" only on newlines and tabs, rather than the default that also includes spaces (spaces are common in data, e.g. file names, while tabs and newlines are not)

The bash strict mode prevents certain bugs, but it can also break certain coding approaches and existing scripts. See the blog post for solutions to such problems, in case you need it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment