Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 1
    this is pretty cool! didn't know pipes could be used like this Commented Jan 14, 2021 at 11:32
  • 1
    @NordineLotfi Strictly speaking, it’s not anything special about pipes here. The magic is all in the /dev/fd paths. On (most) Linux systems, they’re special files that allow access to the file descriptors of the current process in the context they are accessed from. Not sure about the first example, but the second one works because the shell opens the fd for redirection before running the redirected command, and binds that to the requested fd in the command, which means that the shell’s fd 0 is being bound to fd 3 of the pipeline in the second example. Commented Jan 14, 2021 at 23:38
  • @AustinHemmelgarn 1. I don't know what you're trrying to say with "allow access ... in the context they are accessed from" -- I suspect that it's either some banal tautology or something horribly mistaken ;-) Commented Jan 15, 2021 at 9:10
  • @AustinHemmelgarn 2. The shell opening /dev/fd/0 has nothing to do with it whatsoever, it will work fine with the original awk even if /dev/fd/0 was opened by awk: echo hello | original-awk '!length{exit(0)} {print; print substr($0,2) >"/dev/fd/0"; fflush()}'. In that example, I had to do the extra redirection for that silly example because GNU awk (gawk) has a kludge which handles some paths (like /dev/fd/...) internally, instead of passing them to the OS, even on OSs which support such paths. Commented Jan 15, 2021 at 9:13