How to do input/output redirection in Unix/Linux

Redirecting input and output is a fundamental skill for any Bash user. It allows you to control how programs interact with data files and the terminal, making your scripts and commands more flexible and powerful. Here's a detailed explanation of different redirection methods:

Standard Input/Output (stdin/stdout)

  1. stdin (standard input): This is the default source of data for programs. By default, it's the keyboard, where you type in commands.
  2. stdout (standard output): This is where program results are displayed by default. It's usually the terminal screen.

Redirection Operators

Bash offers several redirection operators to manipulate stdin and stdout:

  1. > (right angle bracket): This symbol replaces stdout with a specified file. For example, ls > files.txt saves the output of the ls command to the file files.txt.
  2. >> (double right angle bracket): This appends output to an existing file instead of overwriting it. For example, echo "Line 1" >> log.txt adds "Line 1" to the end of the file log.txt.
  3. < (left angle bracket): This redirects data from a file to stdin. For example, sort < data.csv sorts the contents of the file data.csv.
  4. | (pipe): This operator connects the stdout of one command to the stdin of another. For example, grep "error" log.txt | wc -l counts the number of lines containing "error" in the file log.txt.


Redirecting Standard Output (stdout):

>: Redirects standard output to a file, overwriting the file if it already exists.
command > output.txt
>>: Redirects standard output to a file, appending the output to the end of the file if it already exists.
command >> output.txt
Redirecting Standard Error (stderr):

2>: Redirects standard error to a file.
command 2> error.txt
2>>: Redirects standard error to a file, appending to the end of the file.
command 2>> error.txt
Redirecting Both Standard Output and Standard Error:

&> or >&: Redirects both standard output and standard error to a file.
command &> output_and_error.txt
Redirecting Standard Input (stdin):

<: Redirects standard input from a file.
command < input.txt
<<: Here document, a way to provide input inline in the script or command.
command << EOF input_data EOF
Combining Input and Output Redirection:

< and > can be combined to both read from and write to files.
command < input.txt > output.txt
Redirecting Output and Error to Different Files:
ls -l /nonexistent 1> output.txt 2> error.txt
Redirecting Output and Error to the Same File:
ls -l /nonexistent &> output_and_error.txt
Reading Input from a File:
while read line; do echo "Line: $line" done < input.txt

Redirection Caveats

  1. Redirecting output overwrites existing files by default. Use >> for appending.
  2. Ensure file permissions allow writing before redirecting output.
  3. Redirected commands don't display output on the terminal unless explicitly captured.


Input and output redirection are accomplished using special symbols. > and >> redirect standard output, < redirects standard input, and 2> redirects standard error, allowing for efficient control over where a command reads from or writes to in the Bash shell. These features enhance script flexibility and enable efficient data flow manipulation.