Zsh is cool : stream redirections

Zsh extends the usual stream redirections with two nice features …

Process substitutions

How often have you done the following :

  1. Launch one or many commands and redirect their outputs into temporary files
  2. Launch a command which reads and processes those temporary files
  3. Delete the temporary files

If you occasionaly write shell scripts then it is likely that the answer is “many times”. If so, then you’ll be glad to know that Zsh can automate this for you.

Considere the following snippet :
spaghetti% cat <(date) <(uptime) Tue Aug 14 16:08:35 CEST 2007 16:08:35 up 79 days, 18:47, 3 users, load average: 0.16, 0.18, 0.16

What Zsh did here is

  1. Launch date and uptime as background processes
  2. Connect their outputs to a temporary named pipe
  3. Replace the process redirections in the command line by the named pipes
  4. Launch cat with the modified command line
  5. Dispose the named pipes

That is just what you wanted ... packed in a handy one-liner.

Multiple redirections

If you've already needed to redirect the output of a command to multiple files, thn you already know the "tee" command. But with Zsh you don't need tee anymore, because Zsh has it built-in.

See the following snippet :
spaghetti% uptime >test.txt >test.txt2
spaghetti% cat test.txt
16:54:47 up 79 days, 19:33, 3 users, load average: 0.22, 0.15, 0.15
spaghetti% cat test.txt2
16:54:47 up 79 days, 19:33, 3 users, load average: 0.22, 0.15, 0.15
spaghetti%

As you can see, output was duplicated to both files.

If you liked this article about Zsh, then you might be interested in my previous article about brace expansion in zsh.

Leave a Reply

Your email address will not be published. Required fields are marked *