Standard Input and Output Redirection

A comprehensive guide to understanding and using standard input/output redirection in Unix and Linux shells

This post explains how the shell and Unix commands handle input/output streams and how to redirect them.

Overview

“The shell and many UNIX commands take their input from standard input (stdin), write output to standard output (stdout), and write error output to standard error (stderr).”

By default, stdin connects to the keyboard while stdout and stderr display on the terminal screen. File redirection uses metacharacters to specify alternative destinations.

C Shell Family

Key redirection operators:

CharacterAction
>Redirect standard output
>&Redirect stdout and stderr
!Redirect stdout; overwrite existing file
>&!Redirect stdout/stderr; force overwrite
|Pipe stdout to another command
>>Append to stdout
>>&Append stdout and stderr

Example commands:

  • who > names — save output to file
  • (pwd; ls -l) > out — redirect multiple commands
  • who >& /dev/null — suppress all output

Bourne Shell Family

Uses numeric file descriptors (0=stdin, 1=stdout, 2=stderr):

CharacterAction
>Redirect stdout
2>Redirect stderr
2>&1Redirect stderr to stdout
>>Append stdout
2>&1|Pipe both streams

Example commands:

  • who > names — redirect output
  • who 2> /dev/null — suppress errors only
  • cat file > out 2> error — separate output and error files

Source

Reference material adapted from Math/CS at University of Picardie

comments powered by Disqus

© 2025 Santosh Manoharan