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

pull/278/head
Joshua Levy 9 years ago
parent ce2e0471da
commit c4ebf33036

@ -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`.
- 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.
```bash
@ -118,7 +122,7 @@ Notes:
- In Bash, note there are lots of kinds of variable expansion. Checking a variable exists: `${name:?error message}`. For example, if a Bash script requires a single argument, just write `input_file=${1:?usage: $0 input_file}`. Arithmetic expansion: `i=$(( (i + 1) % 5 ))`. Sequences: `{1..10}`. Trimming of strings: `${var%suffix}` and `${var#prefix}`. For example if `var=foo.pdf`, then `echo ${var%.pdf}.txt` prints `foo.txt`.
- Brace expansion using `{`...`}` can reduce having to re-type similar text and automate combinations of items. This is helpful in examples like `mv foo.{txt,pdf} some-dir` (which moves both files), `cp somefile{,.bak}` (which expands to
- Brace expansion using `{`...`}` can reduce having to re-type similar text and automate combinations of items. This is helpful in examples like `mv foo.{txt,pdf} some-dir` (which moves both files), `cp somefile{,.bak}` (which expands to
`cp somefile somefile.bak`) or `mkdir -p test-{a,b,c}/subtest-{1,2,3}` (which expands all possible combinations and creates a directory tree).
- The output of a command can be treated like a file via `<(some command)`. For example, compare local `/etc/hosts` with a remote one:

Loading…
Cancel
Save