Redirecting Output in Scripts
You can use the STDOUT
and STDERR
file descriptors in your scripts to produce output in multiple locations simply by redirecting the appropriate file descriptors. There are two methods for redirecting output in the script:
- Temporarily redirecting each line
- Permanently redirecting all commands in the script
The following sections describe how each of these methods works.
Temporary redirections
If you want to purposely generate error messages in your script, you can redirect an individual output line to STDERR
. You just need to use the output redirection symbol to redirect the output to the STDERR
file descriptor. When you redirect to a file descriptor, you must precede the file descriptor number with an ampersand (&
):
echo "This is an error message" >&2
This line displays the text wherever the STDERR
file descriptor for the script is pointing, instead of the normal STDOUT
. The following is an example of a script that uses this feature...