Chan Kim Chan Kim - 1 year ago 29
Bash Question

cannot understand combined exec and redirection in bash script

I know exec is for executing a program in current process as quoted down from here

exec replaces the current program in the current process, without
forking a new process. It is not something you would use in every
script you write, but it comes in handy on occasion.

I'm looking at a bash script a line of which I can't understand exactly.

exec &> >(tee -a "$LOG")
echo Logging output to "$LOG"

Here, exec doesn't have any program name to run. what does it mean? and it seems to be capturing the execution output to a log file. I would understand if it was
exec program |& tee log.txt
but here, I cannot understand
exec &> >(tee -a log.txt)
. why another

What's the meaning of the line? (I know -a option is for appending and
is for redirecting including stderr)

EDIT : after I selected the solution, I found the
exec &> >(tee -a "$LOG")
works when it is bash shell(not sh). So I modified the initial
. But
exec &>> "$LOG"
works both for bash and sh.

Answer Source

From the manual:

exec [-cl] [-a name] [command [arguments]]

If command is not specified, any redirections take effect in the current shell, [...]

And the rest:

&>     # redirects stdout and stderr
>(cmd) # redirects to a process

See process substitution.