11

I saw here and here too the following construction:

exec > >(tee -a script.log)

I know what the tee command is, and the (command...) usually means execute the command in a subshell, and exec replaces the current shell with a program, like exec ls, (but here there is no command) and additionally what is meant with the > >?

Can anybody clarify this dark wizzardy?

exec >{space}> (command)

@Seth? :) Any pointer where i can read more about this magic would be appreciated. :)

Community
  • 1
  • 1
clt60
  • 62,119
  • 17
  • 107
  • 194

1 Answers1

7

It replaces the current bash session with another, and writes the output of all commands to script.log.

In that way, you can use your bash shell normally, and you wouldn't see any difference (mostly), but all output is going to show up on your screen and in the script.log file.

From exec manpages:

If command is supplied, it replaces the shell without creating a new process. If no command is specified, redirections may be used to affect the current shell environment.

The >(tee -a script.log) magic creates a pipe, so instead of writing to a file like we would (with >> script.log in this case), we write to the process tee -a script.log, which does the same. For some reason unbeknown to me, using >> does not work, but writing to the named pipe works. Source here

evgeny
  • 2,564
  • 17
  • 27
  • So, with other words i can have more redirections? Like exec >(somecomamnd) >(anothercommand) >(thirdone) > [empty mean STDOUT?] ? (and as i told, i know tee :) – clt60 May 28 '11 at 11:52
  • you can also read from multiple things, for example `diff <(ls /one) <(ls /two)` – evgeny May 28 '11 at 11:54
  • 1
    This is really very cool. Can make several pipe-like flows. Extremely powerful addition to cmd >somewhere 2>error and so on. I'm happy to learn new things. ;) – clt60 May 28 '11 at 12:00