Table of Contents
Understanding Standard Input, Output, and Error
Many command-line tools read from standard input (stdin) and write to standard output (stdout) and standard error (stderr).
Numbered streams:
0– stdin (input to a program)1– stdout (normal output)2– stderr (error messages)
By default:
- stdin usually comes from your keyboard
- stdout and stderr usually go to your terminal screen
Redirection lets you change where these streams come from and go to: files, other commands, or nowhere.
Redirecting Standard Output (`>` and `>>`)
Overwriting a file with `>`
Use > to send stdout to a file, overwriting the file if it exists:
echo "Hello" > greeting.txt
ls > file_list.txtEffects:
- If
greeting.txtdoesn’t exist: it is created - If it exists: its contents are replaced
Common patterns:
uname -a > system-info.txt # Save system info
date > last-run.txt # Record the last time a command was run
Be careful with >: existing data will be lost.
Appending to a file with `>>`
Use >> to add output to the end of a file, preserving existing contents:
echo "Another line" >> notes.txt
ls *.log >> log-files.txtTypical uses:
date >> backup-history.txt
echo "Backup completed" >> backup-history.txtSummary:
>– create or overwrite>>– create or append
Redirecting Standard Input (`<`)
Most commands read from stdin. Instead of typing input manually, you can take it from a file:
wc -l < file.txtThis is roughly equivalent to:
wc -l file.txtbut uses stdin instead of a filename argument.
Another example:
sort < unsorted.txt
You’ll see stdin redirection most with programs that can read from stdin by default, like cat, sort, wc, etc.
Redirecting Standard Error
Some commands write errors to stderr (stream 2). You can redirect it separately from stdout.
Send stderr to a file
command 2> errors.logExamples:
ls /no/such/dir 2> errors.log
Only error messages go into errors.log. Normal output still appears on the screen.
Append errors instead of overwriting:
command 2>> errors.logRedirect stdout and stderr separately
You can use two redirections in one command:
command > output.log 2> errors.log- stdout →
output.log - stderr →
errors.log
Combining stdout and stderr
Sometimes you want both normal output and errors in the same file.
Redirect stderr to the same place as stdout
Bash-style syntax:
command > all.log 2>&1Explanation:
> all.log– send stdout toall.log2>&1– send stderr (2) to where stdout (1) is currently going (the file)
Order matters. These two are different:
- ✅
command > all.log 2>&1 - ❌
command 2>&1 > all.log(stderr is redirected to the old stdout, not the file)
Another variant (Bash-only):
command &> all.log # stdout and stderr both to all.log (overwrite)
command &>> all.log # stdout and stderr both to all.log (append)Discarding Output with `/dev/null`
/dev/null is a “black hole” for data: anything sent there is thrown away.
Common uses:
- Ignore normal output but see errors:
command > /dev/null- Ignore errors but see normal output:
command 2> /dev/null- Ignore everything:
command > /dev/null 2>&1
# or
command &> /dev/nullThis is useful for commands you care about only succeeding, not what they print.
Using Pipes (`|`) to Connect Commands
A pipe sends stdout of one command as stdin to another:
command1 | command2Examples:
ls | less
dmesg | grep usb
ps aux | grep firefoxKey points:
|connects stdout of the left command to stdin of the right command- stderr is not piped by default
To include stderr in the pipe (Bash):
command1 2>&1 | command2
# or
command1 |& command2Example:
make 2>&1 | tee build.logHere-Documents (`<<`) and Here-Strings (`<<<`)
Sometimes you want to feed multiple lines of text directly into a command, without creating a separate file.
Here-document (`<<`)
A here-document lets you embed a block of text in your command line or script.
General pattern:
command <<EOF
line 1
line 2
line 3
EOF
EOF is a delimiter word you choose; it marks the start and end of the block. Any word can be used as long as it matches at both ends.
Examples:
cat <<END
This is line one.
This is line two.
END
Or for tools that read stdin, like wc:
wc -l <<TEXT
first line
second line
third line
TEXTHere-string (`<<<`)
A here-string sends a single string as stdin:
tr 'a-z' 'A-Z' <<< "hello world"Equivalently:
echo "hello world" | tr 'a-z' 'A-Z'Another example:
wc -w <<< "one two three four"Here-strings are Bash-specific and not POSIX sh.
Combining Redirection and Pipes
You can mix and match redirections and pipes in one command line.
Examples:
- Save command output and view it:
ls -l | tee files.log
tee reads stdin and writes it to both stdout (screen) and a file.
- Append and ignore errors:
some_command >> results.txt 2> /dev/null- Pipe and log stderr:
some_command 2> errors.log | grep "success"Order of redirections and pipes generally follows:
- Command and its arguments
- Pipe(s)
- Redirections
Practical Examples
Save a directory listing and view it later
ls -l > dir-list.txt
less dir-list.txtCount only error messages from a program
program 1> /dev/null 2> errors.log
wc -l errors.logLog both output and errors while still seeing them
program 2>&1 | tee program.logRun a quiet command, only react on exit status
backup.sh > /dev/null 2>&1
echo "Backup completed with status $?"These patterns are the foundation for automating tasks and chaining powerful command-line workflows.