1

in my bash script I use the following approach in order to write every thing from standard output and standard error to log.txt

export LOG=/tmp/installation/log.txt

exec > $LOG 2>&1

so everything is writing to /tmp/installation/log.txt

but how to do the writing to two different logs path

lets say I want to write the same standard output and standard error both to 2 log files

then how is the syntax in this case ?

example

export LOG=/tmp/installation/log.txt
export LOG_I=/tmp/installation/log_info.txt

so both logs will get the same content

can we do like this?

export LOG=/tmp/installation/log.txt
export LOG_I=/tmp/installation/log_info.txt

exec > $LOG $LOG_I 2>&1
1

Your command

exec > $LOG $LOG_I 2>&1

would not quite work as it's the same as

exec $LOG_I > $LOG 2>&1

I.e., it would try to exec the command given by the unquoted expansion of $LOG_I.


To put data into multiple files, you will have to duplicate it. This is done with tee.

For example,

log1="/tmp/logfile-1.txt"
log2="/tmp/logfile-2.txt"

exec > >( tee "$log1" >"$log2" ) 2>&1

This uses a process substitution to call tee for the standard output stream. The tee utility would take its input and save it to $log1 and then redirect a copy of it to $log2 (tee always produces a copy of its input on its standard output, but you could also have used tee "$log1" "$log2" >/dev/null). Then we redirect our standard error stream to the same place as our standard output, so that tee may duplicate it in the same way.

To append to both logs, you would use

exec > >( tee -a "$log1" >>"$log2" ) 2>&1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.