Lesson 1 · Unit 02
Three streams
stdout, stderr and stdin, and how >, >>, 2> and | route them
What you practise
- Stdin, stdout, stderr
>,>>,2>,2>&1,<,/dev/nullcat,head,tail -f,wc,echo
Try a question from this lesson
Pick one
> sends what a command prints into a file instead of the terminal. Both lines write to the same file. What does cat show?
echo hello > out.txt
echo world > out.txt
cat out.txtCorrect answer
world
Why
> empties the file before writing, every time. The second line wipes hello and leaves only world. To keep what is already there, you need a different operator.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Three streams
Every command starts with three numbered streams. stdin (0) is its input. stdout (1) carries results. stderr (2) carries complaints.
Both outputs appear on your terminal, which is why they look alike until you redirect one.
> redirects stdout, 2> redirects stderr, and < feeds a file to stdin.
┌─────────┐
0 stdin ──▶ │ command │ ──▶ 1 stdout
└─────────┘ ──▶ 2 stderrConcept
The pipe
a | b connects the stdout of a straight to the stdin of b. No file in between.
Only stdout goes down the pipe. Errors still reach the terminal.
Three small tools you will pipe into all the time: head keeps the first lines, tail the last ones, and wc -l counts them.
ls /etc | wc -l # count entries
ls /etc | head -n 3 # first three
tail -n 3 app.log # last three lines
tail -f app.log # keep following9 more exercises in the app
- 1× fill the blanks
- 3× multiple choice
- 3× type the answer
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.