Shell redirection

What is shell redirection?

It is a way of redirecting the standard input and/or the standard output of a program before it is executed by the shell.

What is the standard input of a program?

By default, a program run from a shell has only one standard input:

Name File descriptor Description Default source
Standard Input (STDIN) 0 Input data consumed by the program. Keyboard
What is the standard output of a program?

By default, a program run from a shell has 2 standard outputs:

Name File descriptor Description Default destination
Standard Output (STDOUT) 1 Normal output generated by the program. Terminal
Standard Error (STDERR) 2 Error messages generated by the program. Terminal
How can I send the output of a program to a file?

The redirection operator > sends the standard output of a program to a file. If the file does not exist, it will be created. If it does exist, it will be overwritten.

This example will write the standard output of the whoami command to the file output.txt.

whoami > output.txt
whoami usage

Usage: whoami

Example: whoami

This example returns the username of the current user.

Can I append the output of a program to a file instead of overwriting it?

Yes, the redirection operator >> appends the output of a program to the end of a file. If the file does not exist, it will be created.

whoami >> output.txt
How can I send the error output of a program to a file?

You can use 2> to send the standard error output of a program to a file. The number 2 is the file descriptor number for STDERR. You can learn more about file descriptors in this guide.

This example will write the standard error output of the ls command to the file errors.txt.

ls ./this-directory-does-not-exist 2> errors.txt
How can I use a file as the input of a program?

You can use the redirection operator < to read the content of a file as the input of a program.

This example will create a file called input.txt with the content there are five words here.

echo there are five words here > input.txt
echo usage

Usage: echo [OPTIONS] STRING

Example: echo Hello, World!

This example writes the string Hello, World! to the standard output.

Common options:

  • -n: do not print the trailing newline character.

The file input.txt will be used as the standard input of the wc command to count the number of words in the file.

wc -w < input.txt
wc usage

Usage: wc [OPTIONS] FILE

Example: wc -l input.txt

This example counts the number of lines in the file input.txt.

Common options:

  • -l: shows the number of lines.
  • -w: shows the number of words.
  • -c: shows the number of bytes.
Can I do both input and output redirection at the same time?

Yes, you can even redirect all of them at once if you want to.

wc -w < input.txt > output.txt 2> errors.txt
Can I redirect STDERR to the same destination as STDOUT?

Yes, you can. The redirection operator 2>&1 sends the output of STDERR (2) to the same destination as STDOUT (1). If STDOUT is redirected to a file, this needs to go after the redirection operator for STDOUT, otherwise it won't work.

ls ./this-directory-does-not-exist > output.txt 2>&1
Can I discard any of the standard outputs of a program?

Yes, you can redirect any of the standard outputs you want to discard to the null device (/dev/null). This will effectively ignore the redirected output without giving any error messages.

whoami > /dev/null
Where can I find more info about shell redirects?

External links: