Linux Command Reference ยท Concepts
stdin, stdout and stderr
Understand Linux standard input, output and error: file descriptors 0, 1 and 2, redirecting each, merging with 2>&1, pipes, tee and common ordering mistakes.
Every Unix program starts with three open channels. Standard input (stdin, descriptor 0) is where it reads data from, standard output (stdout, 1) is where it writes its normal results, and standard error (stderr, 2) is where it writes diagnostics. By default all three are connected to your terminal, but the shell can attach them to files, pipes or devices without the program knowing.
Keeping results and complaints on separate channels is what makes the shell composable: sort data.txt > sorted.txt stores the results while error messages still appear on screen, and a pipe passes only stdout to the next program. The numbers are plain file descriptors.
program < input.txt > output.txt 2> errors.txt program 2>&1 | less program > /dev/null 2>&1
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
How the streams behave
Defaults and tty detection
A program does not know where its streams lead, but it can ask whether they are a terminal (isatty). That is why ls prints columns and colors on screen but one name per line in a pipe, and why grep shows colors only when output is a terminal.
Redirection forms
| Form | Effect |
|---|---|
< file |
stdin reads from file. |
> file, >> file |
stdout writes to file (truncate or append). |
2> file |
stderr to file. |
&> file, &>> file |
Bash shorthand: both stdout and stderr (same as > file 2>&1). |
2>&1 |
Make stderr go wherever stdout currently goes. |
1>&2 or >&2 |
Send stdout to stderr, handy for error messages in scripts. |
| |
Connect stdout to the next command's stdin. |
|& |
Bash: connect stdout and stderr to the next command. |
<<< string, << EOF |
Provide stdin from a string or a here-document. |
> /dev/null |
Discard output; see /dev/null. |
Errors and edge cases: cmd > file truncates file before cmd runs, so sort f > f empties the file. Use a temporary file, sponge, or set -o noclobber to refuse overwrites.
Usage notes
- Write errors to stderr in your scripts:
echo "usage: ..." >&2. Then callers can capture results without error text mixed in. - Exit status is separate from both streams: a program can print nothing and fail, or print an error and still exit 0. See Exit codes cheat sheet.
- Buffering. stdout to a pipe or file is usually buffered, stderr is not, so interleaving can look out of order. Tools offer line buffering (
stdbuf -oL,grep --line-buffered). - Reading stdin interactively works with
read; a program waiting on stdin seems to hang when you forgot the file argument. Press Ctrl+D to send end-of-file. - Pipelines connect only stdout. To process errors too, use
2>&1 |or|&. - Capture separately:
out=$(cmd 2>err.txt)stores stdout in a variable and stderr in a file. - Silencing:
cmd >/dev/null 2>&1hides everything, which also hides real problems; prefer2>/dev/nullfor a known noisy message. - The terminal device is
/dev/tty; use it to prompt a user even when stdin and stdout are redirected.