mirror of
https://github.com/xmengnet/the-art-of-command-line.git
synced 2024-12-25 00:46:30 +08:00
Cover set -v, set -u, and more on trap.
This commit is contained in:
parent
ce2e0471da
commit
c4ebf33036
1 changed files with 6 additions and 2 deletions
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue