Most Unix/Linux system commands accept input from your terminal and return the resulting output to it.
A command normally reads its input from the standard input (stdin), which is usually your terminal. Similarly, by default, a command writes its output to standard output (stdout), which is also your terminal.
The concept of redirection refers to the ability to redirect stdin, stdout, and stderr from their usual locations to another file, command, or even peripheral devices.
When a program is executed in Bash or another Linux shell, it uses three different I/O streams. Each stream is represented by a numerical file descriptor:
0 – stdin (standard input)
1 – stdout (standard output)
2 – stderr (standard error)
A file descriptor is simply a number that uniquely identifies an open file in the operating system.
The input stream sends data to the program, typically by typing on the keyboard.
The output stream sends program output back to the terminal, and the error stream sends any error messages.
Both output and error messages are printed to the terminal screen by default.
Using the piping technique, you can direct the output of one program into the input of another program, acting as the standard input.
Redirecting Standard Output (stdout)
Redirection is a method of capturing a program's output and sending it as input to another program or file.
The n>
operator, where n
is the file descriptor number, is used to redirect streams.
When n
is not specified, the standard output stream (1
) is assumed.
These two commands are identical — both redirect stdout to a file:
$ echo "Linux is the future" > linux.txt
$ echo "Linux is the future" 1> linux.txt
Redirecting Standard Error (stderr)
You can redirect the standard error (stderr) using the 2>
operator. Here's an example:
$ ping badhost.com 2> error.txt
This will redirect the error output produced by the command to a file called error.txt
.
You can also redirect both stdout and stderr into separate files:
$ cmd 2> error.txt > output.txt
This saves the error output to error.txt
and the standard output to output.txt
.
If you want to suppress error messages completely, redirect stderr to a special device called /dev/null
:
$ cmd 2>/dev/null
Redirecting stderr to stdout
When saving both standard output and error output into a single file, you can redirect stderr to stdout.
$ cmd > file.txt 2>&1
Here:
> file.txt
redirects stdout tofile.txt
.2>&1
redirects stderr to wherever stdout is currently going (the file).
Important:
The order of redirection matters. If you reverse it like this:
$ cmd 2>&1 > file.txt
then only stdout will be redirected to the file. stderr will still print to the terminal because it is redirected to stdout before stdout is changed.
Another way to redirect both stdout and stderr together is using &>
:
$ cmd &> file.txt
In Bash, &>
is functionally the same as 2>&1
.
Redirecting Standard Input (stdin)
You can use stdin redirection to pass the contents of a text file to a command:
$ wc < file.txt
While stdin redirection is rarely used (because most Linux commands accept filenames directly as arguments), it can sometimes be cleaner and more efficient than using pipes.
For example, instead of doing:
$ cat file.txt | wc
you can just use:
$ wc < file.txt
No need for cat
when the command already accepts input.
That’s a Wrap
Thanks for sticking with us through this guide. I hope you found it useful!
If you found this guide helpful:
Subscribe for more weekly guides on Linux, sysadmin, and DevOps
Like and restack so that other Linux users can find it too.