Subshells, pipelines and errors
Quick answer
Automation needs explicit failure handling. A subshell has its own shell state, and a pipeline can hide an earlier command failure unless you inspect the relevant statuses.
Prepare the practice environment
Run python3 lab-fixtures/shell-scripting/setup.py as an ordinary user from the extracted package root in a disposable Linux environment. Enter the printed directory, check pwd, then run export LC_ALL=C; umask 077; set -o pipefail. Use a fresh fixture for each example. Save the code as example.sh inside that directory and run bash --noprofile --norc -o pipefail example.sh rather than sourcing it. Stop if a command fails unexpectedly. All output paths must be new. The source of check-report.sh is in the fixture JSON. Outputs below are not commands.
How it works
Parentheses ( commands ) run in a subshell environment; variable assignments there do not persist in the caller. By default Bash reports the last command status in a pipeline. With set -o pipefail, it reports the rightmost nonzero status, or zero when all succeed. PIPESTATUS stores individual statuses and must be captured immediately. set -e has documented exceptions, including tested commands in if conditions; it is not a complete error policy. Use explicit checks on important operations. In default Bash mode, most pipeline components run in subshells; lastpipe can alter the last component behavior.
For false | true, the individual statuses are 1 and 0, even when the pipeline as a whole reports success in default mode. Save PIPESTATUS immediately, for example with parts=("${PIPESTATUS[@]}"); even an intervening printf changes the recorded array. pipefail changes the combined result, not the commands themselves. Choose which failures are expected and handle those explicitly rather than assuming every nonzero value is an emergency.
Try the examples
mode=parent( mode=child; printf '%s\n' "$mode" )printf '%s\n' "$mode"The child assignment does not change mode in the parent.
Expected stdout:
childparent(set +o pipefail; false | true; printf 'default=%s\n' "$?")(set -o pipefail; false | true; printf 'pipefail=%s\n' "$?")Each subshell uses its own option setting.
Expected stdout:
default=0pipefail=1set -eif false; then printf 'unexpected\n'else printf 'handled failure\n'fiprintf 'still running\n'The failure is tested by if, so errexit does not terminate this script.
Expected stdout:
handled failurestill runningCommon mistakes
Adding set -euo pipefail is not a substitute for deciding what each failure means. Do not assume state assigned in a pipeline survives.
Practice
Compare false | true with and without pipefail. Explain why an if condition can fail while errexit is enabled.
Related references
Study: bash. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Subshells, pipelines and errors — Knowledge Check
Apply the lesson concepts to the controlled shell fixture.
Default status of false | true?
What does pipefail report if all commands succeed?
Does set -e always exit on false in an if condition?
Does a subshell variable assignment persist in its parent?