A Linux shell, such as Bash, receives input and sends output as sequences or streams of characters. Each character is independent of the one before it and the one after it. The characters are not organized into structured records or fixed-size blocks. Streams are accessed using file IO techniques, whether or not the actual stream of characters comes from or goes to a file, a keyboard, a window on a display, or some other IO device. Linux shells use three standard I/O streams, each of which is associated with a well-known file descriptor:
If you already studied the article "Learn Linux 101: Text streams and filters," some of the material in this article will be familiar to you.
Redirecting output
There are two ways to redirect output to a file:
n>
redirects output from file descriptor n to a file. You must have write authority to the file. If the file does not exist, it is created. If it does exist, the existing contents are usually lost without any warning.
n>>
also redirects output from file descriptor n to a file. Again, you must have write authority to the file. If the file does not exist, it is created. If it does exist, the output is appended to the existing file.
The n in n> or n>> refers to the file descriptor. If it omitted, then standard output is assumed. Listing 3 illustrates using redirection to separate the standard output and standard error from the ls command using files we created earlier in our lpi103-4 directory. We also illustrate appending output to existing files.
Listing 3. Output redirection
[ian@echidna lpi103-4]$ ls x* z*
ls: cannot access z*: No such file or directory
xaa xab
[ian@echidna lpi103-4]$ ls x* z* >stdout.txt 2>stderr.txt
[ian@echidna lpi103-4]$ ls w* y*
ls: cannot access w*: No such file or directory
yaa yab
[ian@echidna lpi103-4]$ ls w* y* >>stdout.txt 2>>stderr.txt
[ian@echidna lpi103-4]$ cat stdout.txt
xaa xab yaa yab
[ian@echidna lpi103-4]$ cat stderr.txt
ls: cannot access z*: No such file or directory
ls: cannot access w*: No such file or directory
We said that output redirection using n> usually overwrites existing files. You can control this with the noclobber option of theset builtin. If it has been set, you can override it using n>| as shown in Listing 4.
Listing 4. Output redirection with noclobber
[ian@echidna lpi103-4]$ set -o noclobber
[ian@echidna lpi103-4]$ ls x* z* >stdout.txt 2>stderr.txt
-bash: stdout.txt: cannot overwrite existing file
[ian@echidna lpi103-4]$ ls x* z* >|stdout.txt 2>|stderr.txt
[ian@echidna lpi103-4]$ cat stdout.txt
xaa xab
[ian@echidna lpi103-4]$ cat stderr.txt
ls: cannot access z*: No such file or directory
[ian@echidna lpi103-4]$ set +o noclobber #restore original noclobber setting
Sometimes you may want to redirect both standard output and standard error into a file. This is often done for automated processes or background jobs so that you can review the output later. Use &> or &>> to redirect both standard output and standard error to the same place. Another way of doing this is to redirect file descriptor n and then redirect file descriptor m to the same place using the construct m>&n or m>>&n. The order in which outputs are redirected is important. For example, command 2>&1 >output.txt is not the same ascommand >output.txt 2>&1 In the first case, stderr is redirected to the current location of stdout and then stdout is redirected to output.txt, but this second redirection affects only stdout, not stderr. In the second case, stderr is redirected to the current location of stdout and that is output.txt. We illustrate these redirections in Listing 5. Notice in the last command that standard output was redirected after standard error, so the standard error output still goes to the terminal window.
Listing 5. Redirecting two streams to one file
[ian@echidna lpi103-4]$ ls x* z* &>output.txt
[ian@echidna lpi103-4]$ cat output.txt
ls: cannot access z*: No such file or directory
xaa xab
[ian@echidna lpi103-4]$ ls x* z* >output.txt 2>&1
[ian@echidna lpi103-4]$ cat output.txt
ls: cannot access z*: No such file or directory
xaa xab
[ian@echidna lpi103-4]$ ls x* z* 2>&1 >output.txt # stderr does not go to output.txt
ls: cannot access z*: No such file or directory
[ian@echidna lpi103-4]$ cat output.txt
xaa xab
At other times you may want to ignore either standard output or standard error entirely. To do this, redirect the appropriate stream to the empty file, /dev/null. Listing 6 shows how to ignore error output from the ls command, and also use the cat command to show you that /dev/null is, indeed, empty.
Listing 6. Ignoring output using /dev/null
[ian@echidna lpi103-4]$ ls x* z* 2>/dev/null
xaa xab [ian@echidna lpi103-4]$ cat /dev/null
Redirecting input
Just as we can redirect the stdout and stderr streams, so too can we redirect stdin from a file, using the < operator. If you already studied the article "Learn Linux 101: Text streams and filters," you may recall, in our discussion of sort and uniq that we used thetr command to replace the spaces in our text1 file with tabs. In that example we used the output from the cat command to create standard input for the tr command. Instead of needlessly calling cat, we can now use input redirection to translate the spaces to tabs, as shown in Listing 7.
Listing 7. Input redirection
[ian@echidna lpi103-4]$ tr ' ' '\t'<text1
1 apple 2 pear 3 banana
Shells, including bash, also have the concept of a here-document, which is another form of input redirection. This uses the << along with a word, such as END, for a marker or sentinel to indicate the end of the input.
- stdout is the standard output stream, which displays output from commands. It has file descriptor 1.
- stderr is the standard error stream, which displays error output from commands. It has file descriptor 2.
- stdin is the standard input stream, which provides input to commands. It has file descriptor 0.
If you already studied the article "Learn Linux 101: Text streams and filters," some of the material in this article will be familiar to you.
Redirecting output
There are two ways to redirect output to a file:
n>
redirects output from file descriptor n to a file. You must have write authority to the file. If the file does not exist, it is created. If it does exist, the existing contents are usually lost without any warning.
n>>
also redirects output from file descriptor n to a file. Again, you must have write authority to the file. If the file does not exist, it is created. If it does exist, the output is appended to the existing file.
The n in n> or n>> refers to the file descriptor. If it omitted, then standard output is assumed. Listing 3 illustrates using redirection to separate the standard output and standard error from the ls command using files we created earlier in our lpi103-4 directory. We also illustrate appending output to existing files.
Listing 3. Output redirection
[ian@echidna lpi103-4]$ ls x* z*
ls: cannot access z*: No such file or directory
xaa xab
[ian@echidna lpi103-4]$ ls x* z* >stdout.txt 2>stderr.txt
[ian@echidna lpi103-4]$ ls w* y*
ls: cannot access w*: No such file or directory
yaa yab
[ian@echidna lpi103-4]$ ls w* y* >>stdout.txt 2>>stderr.txt
[ian@echidna lpi103-4]$ cat stdout.txt
xaa xab yaa yab
[ian@echidna lpi103-4]$ cat stderr.txt
ls: cannot access z*: No such file or directory
ls: cannot access w*: No such file or directory
We said that output redirection using n> usually overwrites existing files. You can control this with the noclobber option of theset builtin. If it has been set, you can override it using n>| as shown in Listing 4.
Listing 4. Output redirection with noclobber
[ian@echidna lpi103-4]$ set -o noclobber
[ian@echidna lpi103-4]$ ls x* z* >stdout.txt 2>stderr.txt
-bash: stdout.txt: cannot overwrite existing file
[ian@echidna lpi103-4]$ ls x* z* >|stdout.txt 2>|stderr.txt
[ian@echidna lpi103-4]$ cat stdout.txt
xaa xab
[ian@echidna lpi103-4]$ cat stderr.txt
ls: cannot access z*: No such file or directory
[ian@echidna lpi103-4]$ set +o noclobber #restore original noclobber setting
Sometimes you may want to redirect both standard output and standard error into a file. This is often done for automated processes or background jobs so that you can review the output later. Use &> or &>> to redirect both standard output and standard error to the same place. Another way of doing this is to redirect file descriptor n and then redirect file descriptor m to the same place using the construct m>&n or m>>&n. The order in which outputs are redirected is important. For example, command 2>&1 >output.txt is not the same ascommand >output.txt 2>&1 In the first case, stderr is redirected to the current location of stdout and then stdout is redirected to output.txt, but this second redirection affects only stdout, not stderr. In the second case, stderr is redirected to the current location of stdout and that is output.txt. We illustrate these redirections in Listing 5. Notice in the last command that standard output was redirected after standard error, so the standard error output still goes to the terminal window.
Listing 5. Redirecting two streams to one file
[ian@echidna lpi103-4]$ ls x* z* &>output.txt
[ian@echidna lpi103-4]$ cat output.txt
ls: cannot access z*: No such file or directory
xaa xab
[ian@echidna lpi103-4]$ ls x* z* >output.txt 2>&1
[ian@echidna lpi103-4]$ cat output.txt
ls: cannot access z*: No such file or directory
xaa xab
[ian@echidna lpi103-4]$ ls x* z* 2>&1 >output.txt # stderr does not go to output.txt
ls: cannot access z*: No such file or directory
[ian@echidna lpi103-4]$ cat output.txt
xaa xab
At other times you may want to ignore either standard output or standard error entirely. To do this, redirect the appropriate stream to the empty file, /dev/null. Listing 6 shows how to ignore error output from the ls command, and also use the cat command to show you that /dev/null is, indeed, empty.
Listing 6. Ignoring output using /dev/null
[ian@echidna lpi103-4]$ ls x* z* 2>/dev/null
xaa xab [ian@echidna lpi103-4]$ cat /dev/null
Redirecting input
Just as we can redirect the stdout and stderr streams, so too can we redirect stdin from a file, using the < operator. If you already studied the article "Learn Linux 101: Text streams and filters," you may recall, in our discussion of sort and uniq that we used thetr command to replace the spaces in our text1 file with tabs. In that example we used the output from the cat command to create standard input for the tr command. Instead of needlessly calling cat, we can now use input redirection to translate the spaces to tabs, as shown in Listing 7.
Listing 7. Input redirection
[ian@echidna lpi103-4]$ tr ' ' '\t'<text1
1 apple 2 pear 3 banana
Shells, including bash, also have the concept of a here-document, which is another form of input redirection. This uses the << along with a word, such as END, for a marker or sentinel to indicate the end of the input.