The stderr stream can be redirected in a similar fashion as stdin or stdout . Figure 4-4 shows what happens when we redirect the stderr.
There is no special symbol for redirecting stderr. The same " > " symbol is used but with the number 2 attached in front of it. If you remember from previous pages, there are three file descriptors opened whenever a command is issued. These file descriptors are shown in Table 4-1.
File Descriptor Number | Description |
---|---|
Standard input | |
1 | Standard output |
2 | Standard error |
We use " 2> " for stderr redirection to tell the shell that we want to redirect the error messages instead of stdout (for which the file descriptor value is 0). Consider the following command.
$ ll xyz xyz not found. $
We tried to list a file with name xyz and the command result shows that this file does not exist. This is an error message of the ll command. Now see the following command.
$ ll xyz >abc xyz not found. $
We tried to redirect the output, but still the message is displayed on our screen. The redirection had no effect because we are trying to redirect stdout while the command is generating stderr messages. Now let us see what happens if we change " > " to " 2> ".
$ ll xyz 2>abc $
Now there is nothing displayed because the error message has been stored in a file with name abc . You can use the cat command to verify that the error message was indeed stored in the abc file.
Top |