Operators
Order of redirection matters
Note that stdout (fd 1) is used by default if none is specified
Eg.
>is equivalent to1>
The following directs both stdout (fd 1) and stderr (fd 2) to the file
dirlist
ls > dirlist 2>&1 While the following directs only the stdout (fd 1) to the file
dirlistThe stderr (fd 2) still points to terminal (orginal value of stdout)
ls 2>&1 > dirlistThe reason being that the shell interprets the values from left to right.
Steps in the first example
> dirlist: Redirect stdout todirlistfile2>&1: Redirect stderr (fd 2) to stdout (&1, fd 1)
Since stdout is pointing to the
dirlistfile, this points stderr to that file too
Steps in the second example
2>&1: Redirect stderr (fd 2) to stdout (fd 1)
However, since stdout is currently pointing to the terminal, the stderr will point to it too
> dirlist: Redirect stdout to thedirlistfile
Overall, stderr points to the terminal, while stdout points to the
dirlistfile
Operators
>: overwrite data in file
Notice that the text bye is replaced by hello
>>: append data to file
Notice that the text hello is appended to the file without overwriting the text bye
<: read from file
Pipe (
|): redirect stdout from one process to the stdin of another
Last updated