Created
September 11, 2025 11:46
-
-
Save janxkoci/36b3a7897375b11360ede2ab22367c86 to your computer and use it in GitHub Desktop.
bash strict mode template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # bash strict mode | |
| # http://redsymbol.net/articles/unofficial-bash-strict-mode/ | |
| set -euo pipefail | |
| IFS=$'\n\t' |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 withset -e, so this option handles the caseIFS=$'\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.