0

The following code:

#!/bin/bash -x
mkfifo pipe 2>/dev/null
tee pipe >/dev/null &
cat pipe

produces no output when run as follows:

$ echo "hi" | ./test.sh
+ mkfifo pipe
+ cat pipe
+ tee pipe
$

Why?! I would expect tee to copy stdin to the named pipe (and /dev/null), and then cat to copy the contents of the named pipe to stdout. Why doesn't it work?!

I'm trying to write a bigger bash script and I really need the tee in there, with something else in the place of /dev/null. I narrowed down the unexpected behaviour to the example above.

Matei David
  • 2,322
  • 3
  • 23
  • 36
  • try changing to `unbuffer cat pipe`. You may have to search for machine for unbuffer, and either call with the full path, or add the path to your PATH variable. OR, rather than echo "hi", cat a pretty big file into your ./test.sh. Good luck. – shellter Jan 16 '13 at 02:07
  • I tried `./test.sh <~/.xsession-errors`, which is about 1Mb, still nothing. – Matei David Jan 16 '13 at 02:26
  • Weirdly, `cat | tee pipe >/dev/null` works as expected. But why?! – Matei David Jan 16 '13 at 02:35

1 Answers1

3

when you background a process its standard input will be set to /dev/null

#!/bin/bash -x
mkfifo pipe 2>/dev/null
cat - | tee pipe >/dev/null &
cat pipe

So you need to specify you want the stdin of the parent, in your case the pipe between echo and ./test.sh