Lessons · Terminal · 2> (errors)
Two streams: output and errors
A command prints normal output on stream 1 and errors on stream 2. > redirects only stream 1; 2> catches the errors; 2>&1 sends both to the same place.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
A script that logs its output but loses its errors is a script whose failure you find out about a week later.
How to think about it
Decide where each stream should go. Errors to a file with 2>, everything to one file with > file 2>&1, and nothing at all to /dev/null.
Worked example
ls missing.txt 2> errors.txtThe complaint goes into errors.txt, not the screen.
cat errors.txtThe error text, mentioning missing.txt.
ls missing.txt > all.txt 2>&1Both streams into one file.
cat all.txtThe same complaint, captured through stream 1's destination.
Your turn
Keep only the errors from the build.
make build-errors.txt
Try it at a real prompt
The trap
Writing 2>&1 > file. The order matters: that sends errors to the screen and output to the file, the opposite of what was meant.
Practise 2> (errors) on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.