Redirecting output to a file
Channel . redirection replaces default channel destinations with file names representing either output files or devices. Using redirection, process output and error messages can be captured as file contents, sent to a device, or discarded.
Redirecting stdout suppresses process output from appearing on the terminal. As seen
in the following table, redirecting only stdout does not suppress stderr error messages
from displaying on the terminal. The special file /dev/null quietly discards channel output
redirected to it.
Usage | Explanation |
---|---|
>file | redirect stdout to a file (*) |
>>file | redirect stdout to a file, append to current file content (**) |
2>file | redirect stderr to a file (*) |
2>/dev/null | discard stderr error messages by redirecting to /dev/null |
&>file | combine stdout and stderr to one file (*) |
>>file 2>&1 | combine stdout and stderr, append to current file content (**) (***) |
Note;
(*) Overwrite existing file, create file if new.
(**) Append existing file, create file if new.
(***) The order of redirection is important to avoid unexpected command behavior. 2>&1 sends stderr to the same place as stdout. For this to work, stdout needs to be redirected first. before adding stderr to stdout. Although &>> is an alternate way to append both stdout and stderr to a file, 2>&1 is the method needed to send both stdout and stderr through a pipe.
Tech info |
Comments
Post a Comment