Because netcat on my box doesn't support -e, The netcat man page gives me this workaround:

$ rm -f /tmp/f; mkfifo /tmp/f
$ cat /tmp/f | /bin/sh -i 2>&1 | nc -l 1234 > /tmp/f

I don't understand how this works. I though that when you try to cat /tmp/f that that would block until something is written to /tmp/f and if that's blocked how does the rest of the command run?


All commands in a pipeline are started simultaneously and run simultaneously – command 2 does not wait for command 1 to exit. Instead, pipelines rely on read operations blocking until the preceding command has produced some output.

For example, the output (stdout) of cat /tmp/f is directly1 connected to the input of /bin/sh -i; whenever sh tries to read from its stdin, it will block until cat has produced some output.

1 (Almost directly; cat's stdout and sh's stdin are tied to two ends of a 'pipe' object.)

  • Wow, the more I learn bash/shell, the more it impresses me. Thank you. – Drew LeSueur Jul 3 '18 at 14:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.