Redirecting output

Thanks to Marcus Hall:

On Mon, Jun 21, 2004 at 04:32:41PM -0400, Angelo Bertolli wrote:
> Using the standard bourn shell script (/bin/sh), is there a way to make
> all output by default direct to another fd besides stdout/stderr?
>
> What I mean is there any way to have the entire script redirect output
> to the same place without having to tack &> onto the end of everything?

Sure!

Try:

exec >file

You can also re-direct stderr with:

exec 2>err_file

That is, the following script will write output to a log file in /tmp:

#!/bin/bash
exec >/tmp/logoutput 2>&1

echo Command executed on $(date)
ls -l / | grep fred
ps -fae >/tmp/ps_output

The output (and stderr) are directed to /tmp/logoutput. The echo and
grep both get their output directed automatically. The ps command
overrides this, and gets re-directed to a different file.


Marcus Hall

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.