17
Nov

Set stderr or stdout Redirection for Entire Script

Here’s a quick to to enable standard error (stderr) redirection for an entire script. Just put this command on the top of your script:

exec 2>> stderr.log

Here’s a quick example script to demonstrate:

#!/bin/sh

exec 2>> stderr.log

echo "Hello There! This stdout statement has been redirected to stderr." 1>&2

The output of cat stderr.log:

Hello There! This stdout statement has been redirected to stderr.

The same thing applies for standard out (stdout), except you use “1” instead of “2”:

exec 1>> stderr.log

Here’s a quick example script to demonstrate:

#!/bin/sh

exec 2>> stderr.log

echo "Hello There! This is a stdout statement."

The output of cat stderr.log:

Hello There! This is a stdout statement.

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 0 Comment So Far

Share your thoughts, leave a comment!