Linux Tutorial / Standard Input, Output, Redirection, and Pipes

A shell command normally has three channels: standard input (stdin), standard output (stdout), and standard error (stderr). In Bash, < supplies file input, > and >> save normal output, and 2> saves errors. A pipe | connects one command's output to another command's input. Tracking these channels explains why some results appear on screen and others do not.

Identify the three standard streams

The three standard file descriptors available to a process are also identified by number. They are commonly attached to your terminal at first, but redirections and pipes can change their destinations.

Number Stream Main role
0 Standard input (stdin) Data the command reads
1 Standard output (stdout) Normal results
2 Standard error (stderr) Diagnostics and errors

The two output streams may appear together in a terminal, but they are separate. Sending normal output to a file with > can therefore leave an error message on screen. The numbers explain syntax such as 2>.

Create a practice file

These examples assume Bash. If ~/linux-course-14-practice exists, choose another name. Do not continue if you cannot create a new directory: > truncates and replaces an existing file.

mkdir ~/linux-course-14-practice
cd ~/linux-course-14-practice
pwd

After verifying your location, save two lines and append a third. >> adds output to the end rather than replacing earlier content.

printf 'alpha\nbeta\n' > source.txt
printf 'gamma\n' >> source.txt
cat source.txt

The final output is alpha, beta, and gamma in order. Decide whether you mean to replace or append before choosing the operator.

Supply input or save normal output

< connects a file to standard input. wc -l < source.txt feeds the stream instead of passing a filename argument, so its output is just the number 3. By contrast, wc -l source.txt normally adds the filename after the number.

wc -l < source.txt

Use > to save normal output. After the next command, the matching line is written to matches.txt instead of being printed to the terminal.

grep 'beta' source.txt > matches.txt
cat matches.txt

cat matches.txt should print beta. A redirection target is opened before the command proper runs; mistyping an important existing filename after > can erase its earlier contents.

Handle errors separately

When ls cannot find a file, it writes a diagnostic to standard error. The following command saves only that stream in errors.txt. It assumes absent-14.txt does not exist in the new practice directory.

ls absent-14.txt 2> errors.txt
cat errors.txt

Wording depends on locale, but the message should say the file is missing. To combine normal output and errors in one file, Bash can use command > all.txt 2>&1. The 2>&1 makes standard error refer to the current destination of standard output.

ls absent-14.txt > all.txt 2>&1
cat all.txt

Redirections are processed left to right. Reversing the order to 2>&1 > all.txt is not equivalent: standard error may remain connected to the terminal, which was standard output's earlier destination.

Connect two commands with a pipe

A pipe | takes the left command's standard output and supplies it as the right command's standard input. This example finds lines containing beta and counts them:

grep 'beta' source.txt | wc -l

The practice file yields 1. A plain pipe does not also send the left command's standard error. By default, Bash reports the last command's exit status for a pipeline, so if grep has no match but wc -l successfully prints 0, the pipeline can still report success. When failures at earlier stages matter, consider a separate check or Bash's pipefail setting.

Redirection changes where a command's streams go; a pipe connects two commands. When the result is surprising, run each stage on its own to determine which stream carries the value or error.

Official documentation

The GNU Bash redirection documentation covers <, >, 2>, and evaluation order. The GNU Bash pipeline documentation covers pipes and their exit status.

More in This Category
Linux column Command: Format Text as a Table

Linux column Command: Format Text as a Table

Learn how to format Text as a Table with the Linux column command, including practical examples, key options, and important precautions.

Linux killall Command: Signal Processes by Name

Linux killall Command: Signal Processes by Name

Learn how to signal Processes by Name with the Linux killall command, including practical examples, key options, and important precautions.

Linux parted Command: Inspect and Manage Partitions

Linux parted Command: Inspect and Manage Partitions

Learn how to inspect and Manage Partitions with the Linux parted command, including practical examples, key options, and important precautions.

Linux split Command: Divide Large Files into Smaller Parts

Linux split Command: Divide Large Files into Smaller Parts

Learn how to divide Large Files into Smaller Parts with the Linux split command, including practical examples, key options, and important precautions.

Linux time Command: Measure Command Execution Time

Linux time Command: Measure Command Execution Time

Learn how to use the Linux time command to measure elapsed, user CPU, and system CPU time for a command, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux umount Command: Unmount a Filesystem Safely

Linux umount Command: Unmount a Filesystem Safely

Learn how to unmount a Filesystem Safely with the Linux umount command, including practical examples, key options, and important precautions.

Linux apk Command: Manage Packages on Alpine Linux

Linux apk Command: Manage Packages on Alpine Linux

Learn how to manage Packages on Alpine Linux with the Linux apk command, including practical examples, key options, and important precautions.

Linux help Command: Read Help for Bash Built-in Commands

Linux help Command: Read Help for Bash Built-in Commands

Learn how to use the Linux help command to read usage information for Bash built-in commands, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux clear Command: Clear the Terminal Display

Linux clear Command: Clear the Terminal Display

Learn how to use the Linux clear command to clear the visible terminal screen and understand scrollback behavior, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux journalctl Command: Read systemd Logs

Linux journalctl Command: Read systemd Logs

Learn how to read systemd Logs with the Linux journalctl command, including practical examples, key options, and important precautions.