#set-euo-pipefail
What does `set -euo pipefail` do and why put it on the first line?
Что отвечать
`-e` exits on any error (a non-zero exit code). `-u` exits when you use an unset variable. `-o pipefail` makes the pipeline's exit code the last NON-zero one rather than the last command's (without it, `false | true` counts as success). These three flags turn bash from a forgiving shell into strict mode that catches most silent bugs right away.
Что хотят услышать
A senior should: - name each flag on its own and give an example where it catches a bug (especially `-u`: without it a typo in a variable name raises no error) - say that `-e` has a catch: `cmd && other` or a command inside `if` will not exit when cmd fails, which is by design - mention `set -x` for tracing and `set -E` (errtrace) so traps are inherited inside functions - name `IFS=$'\n\t'` as the fourth piece of strict mode (protection against word splitting on spaces)
Подводные камни
- ✗ Thinking `set -e` catches ALL errors. No, in conditionals and in a pipe without pipefail it stays quiet.
- ✗ Using unset variables through `${VAR:-default}` without realizing it bypasses `-u`.
- ✗ Leaving strict mode out of production scripts. The silent bug is waiting for its moment.
Follow-up
- ? What does `${VAR:-default}` do, and why does it work under `set -u`?
- ? How does `set -E` differ from `set -e` for trap inheritance?
- ? How does `pipefail` affect the exit code of a multi-stage pipeline?
Глубина в базе знаний