Redirecting output
How to provide standard input to commands, and how to direct output and error messages to the desired destinations. Example:
echo 'hello world' > /dev/null
Commands have three file descriptors associated with them: stdin (0), stdout (1) and stderr (2).
From the source: The GNU bash manual, Redirections.
Redirection theory
On paper, this is how redirection works:
- [n]<file
- Indicates which file stdin should be taken from.
- [n]>file
- Indicates which file stdout should be written to. Unless the noclobber shell option is enabled ( set -o noclobber ), this overwrites the file, even if the command does not write anything to file n.
- [n]>|file
- Like >, but overwrites the file even if the noclobber shell option is enabled.
- [n]>>file
- If the file does not exist, this works like >. If the file exists, the command output is appended to it.
- [n]&>file (and [n]>&file)
- Redirects both stdout and stderr to the indicated file. This is equivalent to 1>file 2>&1.
- [n]&>>file
- Like &>file, but appends to the file. This is equivalent to 1>>file 2>&1.
A couple of notes to these operators:
- For < operators, n defaults to 0 ( stdin).
- For > operators, n defaults to 1 ( stdout).
- Rather than specifying a file, a file descriptor can be given,
prefixed by
&, like this:
command 2>&1
a common mistake is to leave out the & (as in: command 2>1), which will simply redirect stderr to the file ${PWD}/1. - Whitespace is allowed between the operator and the filename ( > file), but not between the file descriptor id and the operator ( 2>file).
Redirection location in commands
Redirections can occur pretty much anywhere on the commandline - the following three - are equivalent. The middle one should probably be avoided, particularly for more complex commands:
>file ls . ls >file . ls . >file
The order of redirections is significant
If the same file descriptor is redirected several times, only the last (right-most) takes effect:
ls . >file1 >file2 # is equivalent to ls . >file2
More subtly, when one file descriptor is redirected into another, the order makes a big difference:
cat ~/.emacs 2>&1 1>file # stderr ends up on stdout, stdout goes to the file
This is not the same as:
cat ~/.emacs 1>file 2>&1 # stdout and stderr both go in the file
The difference being whether 2 is directed to 1 before or after 1 is directed to file.
Writing a message to stderr
Use echo and redirect stdout to stderr:
echo "Wrong input - you are invalid" >&2
Forcing a command into silence ( /dev/null)
Sometimes commands will insist (in the sense that they do not provide a --quiet option) on telling you things you do not care about. For example, cp will complain if a file to be copied does not exist, but if in that case, you want to do nothing, this will silence it:
cp a b &> /dev/null
Make sure there really are no possible errors that you care about.
Pipelines
See the GNU bash manual: Pipelines (operators | and |&)