Sandra Schlichting
Sandra Schlichting

Reputation: 26046

Possible to get all outputs to stdout in a script?

I would like to log all error messages that the commands in a Bash script contains.

The problem is that if I have to add

E=$( ... 2>&1 ); echo $E >> $LOG

to all commands, then the script will become quite hard to read.

Question

Is it somehow possible to get a global variable, so all STDERR becomes STDOUT?

Upvotes: 1

Views: 1091

Answers (2)

eduffy
eduffy

Reputation: 40262

Just start your script with this:

 exec 2>&1

Upvotes: 6

William Pursell
William Pursell

Reputation: 212654

You can do things like:

#!/bin/sh

test -z "$DOLOGGING" && { DOLOGGING=no exec $0 "${@}" 2>&1 | tee log-file; exit; }
...

to duplicate all output/errors to log-file. Although it seems I misread the question and it seems you just want to add exec 2>&1 >/dev/null to the top of your script to print all errors to stdout and discard all output.

Upvotes: 0

Related Questions