Reputation: 2362
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.
Upvotes: 0
Views: 4321
Reputation:
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
Upvotes: 3