What is the difference between `exit` and `return`?
BeginnerConceptFunctions, exit codes, strict mode and traps
Answer
return leaves a function (or a sourced file) with a status; exit terminates the whole shell. In a sourced library, exit kills the user's interactive shell, so libraries should return. Inside $( ) or ( ), exit only ends the subshell.
validate() { [[ -n $1 ]] || return 1; }
validate "$ENV" || { echo "ENV missing" >&2; exit 2; }
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