This document collects basic syntax and best practices for writing BASH scripts.
It is good practice to start BASH scripts with the following lines:
#!/bin/bash set -euo pipefail
set allows you to change shell options. The most useful flags are:
- Stop execution when an error is encountered.
You can “catch” an error to prevent the shell from exiting by appending
|| trueafter a failing command.
- -o pipefail
- Catch errors in pipes. Return zero exit status only if all commands in a pipeline succeed.
- Error out when shell encounters undefined variables by their values.
- Debug mode. Prints every line and output, substituting variables
- Disable file globbing.