Discussion:
[Help-bash] How unnamed pipe is done in bash?
Peng Yu
2017-01-19 21:51:50 UTC
Permalink
Hi, I don't quite understand who unamed pipe is done in bash. It seems
unnamed pipe is mapped to a fd in /dev/fd/<id>. 63 is always used as
the first one. Why the 2nd command won't run correctly?

$ cat <(echo xxx)
xxx
$ cat $(echo <(echo xxx))
cat: /dev/fd/63: Bad file descriptor
$ echo <(echo xxx)
/dev/fd/63
$ echo <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx)
<(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo
xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx)
<(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo
xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx)
<(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo
xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx)
<(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo
xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx)
<(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo
xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx)
<(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo
xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx)
<(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo
xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx)
<(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo xxx) <(echo
xxx)
/dev/fd/63 /dev/fd/62 /dev/fd/61 /dev/fd/60 /dev/fd/59 /dev/fd/58
/dev/fd/57 /dev/fd/56 /dev/fd/55 /dev/fd/54 /dev/fd/53 /dev/fd/52
/dev/fd/51 /dev/fd/50 /dev/fd/49 /dev/fd/48 /dev/fd/47 /dev/fd/46
/dev/fd/45 /dev/fd/44 /dev/fd/43 /dev/fd/42 /dev/fd/41 /dev/fd/40
/dev/fd/39 /dev/fd/38 /dev/fd/37 /dev/fd/36 /dev/fd/35 /dev/fd/34
/dev/fd/33 /dev/fd/32 /dev/fd/31 /dev/fd/30 /dev/fd/29 /dev/fd/28
/dev/fd/27 /dev/fd/26 /dev/fd/25 /dev/fd/24 /dev/fd/23 /dev/fd/22
/dev/fd/21 /dev/fd/20 /dev/fd/19 /dev/fd/18 /dev/fd/17 /dev/fd/16
/dev/fd/15 /dev/fd/14 /dev/fd/13 /dev/fd/12 /dev/fd/11 /dev/fd/10
/dev/fd/9 /dev/fd/8 /dev/fd/7 /dev/fd/6 /dev/fd/5 /dev/fd/3 /dev/fd/4
/dev/fd/64 /dev/fd/65 /dev/fd/66 /dev/fd/67 /dev/fd/68 /dev/fd/69
/dev/fd/70 /dev/fd/71 /dev/fd/72 /dev/fd/73 /dev/fd/74 /dev/fd/75
/dev/fd/76 /dev/fd/77 /dev/fd/78 /dev/fd/79 /dev/fd/80 /dev/fd/81
/dev/fd/82 /dev/fd/83 /dev/fd/84 /dev/fd/85 /dev/fd/86 /dev/fd/87
/dev/fd/88 /dev/fd/89 /dev/fd/90
--
Regards,
Peng
Greg Wooledge
2017-01-19 22:07:36 UTC
Permalink
Post by Peng Yu
Hi, I don't quite understand who unamed pipe is done in bash.
By calling pipe().
Post by Peng Yu
It seems
unnamed pipe is mapped to a fd in /dev/fd/<id>.
That's an operating system-specific implementation detail. A script
should not have to worry about that.
Post by Peng Yu
$ cat <(echo xxx)
Oh, you're actually talking about process substitution, not pipes!

Process substitution can be implemented by using named pipes or by
using /dev/fd/* depending on the target platform. E.g. on Linux it
uses /dev/fd/* and on HP-UX it uses named pipes in /var/tmp.

imadev:~$ uname -a; ls -ld <(echo)
HP-UX imadev B.10.20 A 9000/785 2008897791 two-user license
prw------- 1 wooledg pgmr 0 Jan 19 17:04 /var/tmp//sh-np.a03751

arc3:~$ uname -a; ls -ld <(echo)
Linux arc3 3.16.0-4-686-pae #1 SMP Debian 3.16.39-1 (2016-12-30) i686 GNU/Linux
lr-x------ 1 wooledg voice 64 Jan 19 17:07 /dev/fd/63 -> pipe:[153773]

Again, the back-end implementation details are specific to each target
platform, and your script should *NOT* depend on them unless you are
doing something really low-level.
Chet Ramey
2017-01-19 23:27:58 UTC
Permalink
Post by Peng Yu
Hi, I don't quite understand who unamed pipe is done in bash. It seems
unnamed pipe is mapped to a fd in /dev/fd/<id>. 63 is always used as
the first one. Why the 2nd command won't run correctly?
$ cat <(echo xxx)
xxx
$ cat $(echo <(echo xxx))
cat: /dev/fd/63: Bad file descriptor
Think about the parent-child relationship. The process substitution is
valid in the subshell spawned to run the command substitution. The parent
shell doesn't know anything about it.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://cnswww.cns.cwru.edu/~chet/
Loading...