1

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.

#!/bin/bash
LOG="log.txt"
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 > after &>?
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 #!/bin/sh to #!/bin/bash. But exec &>> "$LOG" works both for bash and sh.

Community
  • 1
  • 1
Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • Does your system have the bash manual page installed? Start it and search for "exec " (exec followed by a space). – Jens Jul 28 '16 at 09:41

1 Answers1

4

From man bash:

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.

agc
  • 7,973
  • 2
  • 29
  • 50
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176