What is the exit status of a pipeline, a subshell, a function and a script?
IntermediateConceptFunctions, exit codes, strict mode and traps
Answer
- Pipeline: status of the last command (or last non-zero with
pipefail);! pipelinenegates it. - Subshell
( ... ): status of its last command or itsexit N. - Function: status of
return Nor its last command. - Script: status of
exit Nor its last command. A script that ends with[[ -n $x ]] && echo okreturns 1 when$xis empty, which surprises cron and CI.
End scripts with an explicit exit 0 (or main "$@" whose last command is meaningful).
More on Functions, exit codes, strict mode and traps
How do you define a function, pass arguments and return a value?BeginnerWhat does `local` do, and what is dynamic scoping?AdvancedWhy is `local out=$(cmd)` a problem under `set -e`?AdvancedWhat are the conventional exit codes, and what do 126, 127, 130, 137 and 143 mean?BeginnerWhat does `set -e` do, and where does it NOT cause an exit?AdvancedExplain `set -euo pipefail` line by line, and add the `-E` flag.Beginner