Jay Taylor's notes

back to listing index

Redirect all subsequent commands' stderr using exec

[web search]
Original source (unix.stackexchange.com)
Tags: bash shell-scripting stream-manipulation unix.stackexchange.com
Clipped on: 2017-11-15

I have a bash file that I need to redirect all output to one file, debug log as well as to the terminal. I need to redirect both stdout and stderr to the debug and log it for all commands in the script.

I do not want to add 2>&1 | tee -a $DEBUG for every single command in the file. I could live with | tee -a $DEBUG.

I remember there was a way to do it with something like exec 2>&1.

Currently I'm using something like the following:

#!/bin/bash
DEBUGLOG=/tmp/debug
exec 2>&1
somecommand | tee -a $DEBUGLOG
somecommand2 | tee -a $DEBUGLOG
somecommand3 | tee -a $DEBUGLOG

but it does not work. Does anyone have a solution/can explain the cause?

Hot Network Questions

site design / logo © 2017 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2017.11.15.27748

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.

This site is not affiliated with Linus Torvalds or The Open Group in any way.