Search code examples
linuxbashpipe

Equivalent for pipe() in bash or other shell


In libc, there is an ability to create an (anonymous) pipe:

https://man7.org/linux/man-pages/man2/pipe.2.html

I am aware of mkfifo in bash and similar shells, and I am aware of the | which allows anonymous piping between the stdout of one process to another's stdin. As per an answer given, also of process substitution. But these are not quite the same functionality as pipe(), which provides a file descriptor for each end of a pipe to be used later or passed on to other processes.

I wondered if there was a trivial equivalent of pipe of which I am unaware?


Solution

  • There is no equivalent to pipe() in shell.

    The closest POSIX and typically used is mktemp with mkfifo.

    In Bash you can do coproc cat and use ${COPROC[0]} and ${COPROC[1]} as a pipe descriptors. Note that you can only run one coproc at a time.

    You can also write a bash builtin that will call a pipe() and set two Bash variables.