[clug] Tee eats the return code of the previous entry in the pipeline

Mark Triggs mst at dishevelled.net
Tue Mar 9 08:30:12 GMT 2004


Michael Still <mikal at stillhq.com> writes:

> I want to be able to run a command, dump it's output to a file and to
> the console (in real time), and still have access to it's return code.
>
> Anyone have any suggestions as to how I might go about achieving that?

Something like this seems to work in bash:

  $ cat test
  #!/bin/bash
  echo foo
  sleep 2
  echo bar
  false
  
  $ ./test | tee blah
  foo
  [.. seconds later..]
  bar
  
  $ echo ${PIPESTATUS[@]}
  1 0

(so ${PIPESTATUS[0]} should give you the return of the first command).

Cheers,

Mark

-- 
Mark Triggs
<mst at dishevelled.net>


More information about the linux mailing list