Standard input, output, error and redirection
Quick answer
Linux commands usually start with three channels: standard input (0), standard output (1) and standard error (2). The shell can connect these channels to files instead of the terminal. This is how you save reports and separate diagnostics from useful data.
Prepare the practice environment
Use the chapter fixture described in lab-fixtures/text-streams/README.md. Run setup as a normal user in a disposable Linux environment, enter the printed directory and check pwd. Every example assumes a fresh fixture. Commands omit the terminal prompt; outputs below are not commands.
How it works
File descriptors are numbered handles to open input/output resources. < reads from a file, > writes stdout to a file (normally creating or truncating it before the command runs), >> appends, and 2> redirects stderr. These operators belong to the shell, not to the invoked program. Exit status is separate from stderr: an informational message can appear on stderr even on success. Redirections are processed from left to right; 2>&1 copies the current destination of descriptor 1 onto descriptor 2, not a permanent link to future changes. In Bash with ordinary settings, command >out 2>&1 puts both streams in out; command 2>&1 >out does not have the same effect.
Imagine stdout and stderr as two separately routed streams. A command can successfully read the first file, write its contents, then fail to open a second file. The report can therefore contain useful but incomplete data. Check the command status immediately and inspect the diagnostic file before accepting the report.
Try the examples
printf '%s\n' 'first' > report.txtprintf '%s\n' 'second' >> report.txtcat report.txtThe first redirection creates/replaces the report, and the second preserves earlier text.
Expected stdout:
firstsecondThe shell opens events.txt as descriptor 0. wc reads from it rather than receiving a filename operand.
Expected stdout:
3cat events.txt missing.txt > normal.txt 2> errors.txtprintf 'status=%s\n' "$?"cat normal.txtwc -l < errors.txtThe missing file is intentional. GNU cat reports failure (1 here), while normal.txt retains successfully read lines and errors.txt receives a diagnostic. $? is read immediately.
Expected stdout:
status=1startcheckfinish1Common mistakes
Never redirect a command into its own input file, for example sort events.txt > events.txt: truncation can happen before the program reads. Use a distinct output file and inspect it first. Do not send all errors to /dev/null just to hide a failure.
Practice
Create a report containing alpha, append beta, then count its lines using wc -l < report.txt. Expect two. Keep source files unchanged.
Related references
Study: cat, tee. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Standard streams and redirection — Knowledge Check
Apply the lesson concepts to the controlled text fixture.
Which descriptor is standard error?
Which operator appends stdout?
Why is sort input.txt > input.txt unsafe?
Does stderr always indicate failure?