Cover set -v, set -u, and more on trap.

This commit is contained in:
Joshua Levy 2015-08-17 00:20:16 -07:00
parent ce2e0471da
commit c4ebf33036

View file

@ -107,7 +107,11 @@ Notes:
- Use `alias` to create shortcuts for commonly used commands. For example, `alias ll='ls -latr'` creates a new alias `ll`. - Use `alias` to create shortcuts for commonly used commands. For example, `alias ll='ls -latr'` creates a new alias `ll`.
- In Bash scripts, use `set -x` for debugging output. Use strict modes whenever possible. Use `set -e` to abort on errors. Use `set -o pipefail` as well, to be strict about errors (though this topic is a bit subtle). For more involved scripts, also use `trap`. - In Bash scripts, use `set -x` (or the variant `set -v`, which logs raw input, including unexpanded variables and comments) for debugging output. Use strict modes whenever possible: Use `set -e` to abort on errors and `set -o pipefail` as to abort within pipes, too (though this topic is a bit subtle). Use `set -u` to detect unset variable usages. For more involved scripts, also use `trap` on EXIT or ERR. A useful habit is to start a script like so, which will make it detect and abort on common errors and print a message:
```bash
set -euo pipefail
trap "echo 'error: Script failed: see last command above'" ERR
```
- In Bash scripts, subshells (written with parentheses) are convenient ways to group commands. A common example is to temporarily move to a different working directory, e.g. - In Bash scripts, subshells (written with parentheses) are convenient ways to group commands. A common example is to temporarily move to a different working directory, e.g.
```bash ```bash