In a shell script, how to redirect stdout to console and both stdout and stderr to a file?

Viewed 1963

I need to redirect stdout to console along with stdout and stderr redirected to a file. This needs to be done inside a shell script. I found the below code to redirect both to console and log file, now I need to remove stderr to console.

exec > >(tee -i "output.log") 2>&1

Could you please help me here?

3 Answers

I think you should launch it like this:

your-command 2>&1 | tee -i "output.log"

It will pipe both stdin and stderr to tee -i output.log, which will echo text before writing.

Or, to ignore stderr (pipe to /dev/null):

your-command 2>/dev/null | tee -i "output.log"

Or your command modified with /dev/null redirection:

exec > >(tee -i "output.log") 2>/dev/null

It'd be something like this, but you don't have to tee that second one.

exec 1> >( tee stdout.txt ) 2> stderr.txt

output 1 is tee'd so you see it on screen, and in stdout.txt
errors 2 just goes straight to stderr.txt

Related