Linux course index · Linux Basics category · Lesson 4 of 8
AI disclosure — AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.
Learning goal
Route input, normal output, and errors, then detect a command failure inside a pipeline.
Concepts and setup
Standard input is descriptor 0, standard output is 1, and standard error is 2. A pipe connects the left command’s standard output to the right command’s input. The shell opens redirection files before executing the command, so redirecting a file onto itself with > can destroy the input. Use a different output file.
This adapts the site’s who > users and numbered-line examples to synthetic data so that every learner can reproduce them.
Create and filter the site’s users-style file
lab=$(mktemp -d)
cd "$lab" || exit
printf '%s\n' 'line1' > users
printf '%s\n' 'line2' >> users
wc -l < users
grep 'line2' users | tee matches.txt
cat matches.txt missing.txt > combined.txt 2> errors.txt
printf 'cat status: %s\n' "$?"
cat combined.txt
head -n 1 errors.txt
Possible output
2
line2
cat status: 1
line2
cat: missing.txt: No such file or directory
Real who output can be empty on some sessions. The missing-file error is deliberate and went to errors.txt. The command still wrote the contents of matches.txt before it failed.
Compare pipeline status in two Bash subshells
( set +o pipefail; false | true; printf '%s\n' "$?" )
( set -o pipefail; false | true; printf '%s\n' "$?" )
Possible output
0
1
Without pipefail, the last command decides the pipeline status. With it, a nonzero component status can be observed. Save $? immediately if you need it.
Questions only
Quick-check questions
- What is the difference between > and >>?
- Does | include standard error by default?
Assessment questions
- Capture regular and error output of ls -d users missing in separate files.
- Explain the result of grep line2 users > users.
- Predict the status of true | false with pipefail off.
Questions and answers
Quick-check answers
- What is the difference between > and >>?
Answer — The first truncates or creates its target. The second appends or creates it. - Does | include standard error by default?
Answer — No. It connects standard output only unless you redirect standard error too.
Assessment answer key
- Capture regular and error output of ls -d users missing in separate files.
Answer — Use ls -d users missing > seen.txt 2> errors.txt and inspect both files. - Explain the result of grep line2 users > users.
Answer — The shell truncates users before grep reads it. Write the result to a different file. - Predict the status of true | false with pipefail off.
Answer — 1 because false is the last command.
Source articles on this site
- ইউনিক্সঃ সেল ইনপুট/আউটপুট রিডাইরেকশন (Unix – Shell Input/Output Redirections)
- ইউনিক্সঃ পাইপ এবং ফিল্টার (Unix – Pipes and Filters)
- 30 mixed quiz questions on Linux Command Line Skills
Continue the course
Previous lesson 03 — Editing Text and Reading Files. Next lesson 05 — Finding Files and Filtering Text.
Linux course index · Linux Basics category · Lesson 4 of 8
