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 sudo Command: Run Commands with Elevated or Another User's Privileges

Linux sudo Command: Run Commands with Elevated or Another User's Privileges

Learn how to use the Linux sudo command to run approved commands with elevated privileges or as another user, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux comm Command: Compare Two Sorted Files

Linux comm Command: Compare Two Sorted Files

Learn how to compare Two Sorted Files with the Linux comm command, including practical examples, key options, and important precautions.

Linux dpkg Command: Inspect and Install Local DEB Packages

Linux dpkg Command: Inspect and Install Local DEB Packages

Learn how to inspect and Install Local DEB Packages with the Linux dpkg command, including practical examples, key options, and important precautions.

Linux Tutorial / Install and Update Packages with APT and DNF

Linux Tutorial / Install and Update Packages with APT and DNF

Compare APT on Debian or Ubuntu with DNF on Rocky Linux for searching, installing, updating, and removing packages, including repository and transaction checks.

Linux apropos Command: Search Commands by Description

Linux apropos Command: Search Commands by Description

Learn how to use the Linux apropos command to find relevant manual pages by searching description keywords, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Understand systemd Services, Boot, and Logs

Linux Tutorial / Understand systemd Services, Boot, and Logs

Understand Linux boot stages and systemd units, distinguish active services from boot enablement, and inspect current-boot logs with systemctl and journalctl.

Bash Command Operators Explained: ;, &&, ||, &, and |

Bash Command Operators Explained: ;, &&, ||, &, and |

Understand how Bash operators run commands sequentially, conditionally, in the background, or through pipelines, with practical examples and precedence guidance.

Linux chown Command: Change File Owner and Group

Linux chown Command: Change File Owner and Group

Learn how to change File Owner and Group with the Linux chown command, including practical examples, key options, and important precautions.

Linux bzip2 Command: Compress a File in bzip2 Format

Linux bzip2 Command: Compress a File in bzip2 Format

Learn how to compress a File in bzip2 Format with the Linux bzip2 command, including practical examples, key options, and important precautions.

Linux su Command: Switch User Accounts and Login Shells

Linux su Command: Switch User Accounts and Login Shells

Learn how to use the Linux su command to switch user accounts and start login or non-login shells, with essential options, practical examples, output interpretation, and common troubleshooting tips.