Streams and Redirection

Separate data from diagnostics and verify the commands that produced them.

Three standard descriptors

Number Stream Role
0 stdin Input
1 stdout Data output
2 stderr Diagnostics
emit_record > report.txt 2> diagnostic.txt
status=$?

Use the complete lesson function. Text, destination, and status are separate parts of its contract.

Redirection order is a trace

Command suffix Descriptor 1 Descriptor 2
>file 2>&1 file file
2>&1 >file file Earlier stdout destination

2>&1 duplicates the current destination; it does not follow later changes to descriptor 1.

An echo-only example cannot demonstrate stderr routing. The lesson emits both streams and checks their files.

Open and close deliberately

exec 3< input.txt
IFS= read -r -u 3 record
exec 3<&-

Use the lesson's guarded subshell and supplied fixture.

3<&- closes descriptor 3. 3<- attempts to open a file named -. Parentheses confine descriptor changes to a subshell.

Partial data can look plausible

producer() { printf 'partial record\n'; return 7; }
producer | cat > report.txt
snapshot=("$?" "${PIPESTATUS[@]}")

With pipefail disabled, expected statuses are pipeline 0, producer 7, consumer 0. Capture all values before another command replaces PIPESTATUS.

The report contains text even though production failed.

pipefail changes the status policy

With set -o pipefail, the same pipeline returns 7.

  • It reports the rightmost nonzero component status.
  • It does not remove partial output or roll back work.
  • A command-specific result such as grep no-match still needs interpretation.

The chapter tests both policies with the same deterministic producer and verifies the files.

State belongs to an execution environment

label=parent
( label=child )
printf '%s\n' "$label"
{ label=group; }
printf '%s\n' "$label"

Expected: parent, then group.

Parentheses create a subshell. Braces group commands in the current shell unless another context, such as a pipeline, changes execution.

Process substitution supplies file-like input

diff <(printf '%s\n' alpha beta) <(printf '%s\n' alpha gamma)

The streams differ at the second record. Diff status 1 means different, not a failure to run. Status 0 means equal; greater than 1 means trouble.

Sorting first asks a different question: equal records regardless of order. Preserve order when it matters.

Comparison success does not certify producers

set -o pipefail
diff <(false) <(true)
printf '%s\n' "$?"

Expected: 0, because the streams are both empty.

Process-substitution producers are outside that foreground pipeline's status collection. For evidence collection, first write owned files with checked commands; compare only after both producers succeed.

Consolidate on your VM

Then complete the graded lab.

Trace descriptors, separate report and diagnostic files, induce an upstream failure, and capture every status immediately. Explain partial output and the limits of pipefail.

Source context: Linuxize redirection and Bash articles linked in the lessons. Fixtures and explanations are original adaptations.