864 B
864 B
Coding guidelines for Bash / shell scripts
- Write all shell scripts for modern Bash — use Bashisms like
&>and[[ ... ]]whenever possible.
Avoid "Bash strict mode"
- By default, don't set any options at the top of the script.
- Add
set -uglobally if the script becomes complex enough (rough guideline: more than 4 variables). - Use
set -o pipefailonly locally when needed.
Executable scripts
If a script is directly executable, it should:
- have no file extension
- use
#!/usr/bin/env bash
If a script is not directly executable, it should have a ".bash" file extension and no shebang line (when possible).
Style guide
- Avoid spaces after redirection operators:
>/dev/null,2>&1,&>"$logfile" - When logging to stderr, prefer to place the
>&2redirection immediately after the command name:printf >&2 %s "Blah blah..."