Interview Prep/Shell Scripting/Functions, exit codes, strict mode and traps

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); ! pipeline negates it.
  • Subshell ( ... ): status of its last command or its exit N.
  • Function: status of return N or its last command.
  • Script: status of exit N or its last command. A script that ends with [[ -n $x ]] && echo ok returns 1 when $x is empty, which surprises cron and CI.

End scripts with an explicit exit 0 (or main "$@" whose last command is meaningful).

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Functions, exit codes, strict mode and traps