There is a Unix code I saw and don't have enough background to fully dechipher:

bash /dev/fd/3 3<< 'EOF'

What is the meaning of 3 3 above? I know what is an heredoc, what is Bash, a device, and fd, but I don't know what is 3 space 3... When I searched the phrase quoted in Google I found basically nothing.

The code was given here as a special heredoc that allows running scrips in-place (specifically those that already include internal heredocs and comments), without creating a file with suitable permissions...

Can a Unix master please explain what is the 3 3 part? Thank you,

  • 2
    where did you see this? you have to give context for people to understand the question. – David Dai Nov 17 '16 at 3:03
  • I've expanded the question and gave as much as details I saw as needed to touch that. – JohnDoea Nov 17 '16 at 3:12

That phrase is a way of passing a here-doc to bash on a file descriptor other than 0. I.e., 3.

The first part of the phrase, base /dev/fd/3, runs bash with file descriptor 3 as its input, and the second part, 3<< EOF, tells the shell to write the here-doc into that file descriptor.

Note that 3<< must be a single token, without spacing.

By doing this, any stdin reading within the here-doc script will read true input (from file descriptor 0), and not the here-doc itself. As I understand it, without doing something like this, the here-doc ends up being stdin for the inner bash, and in that case it cannot read "true input".

  • Thank you for the answer. File descriptor? Rather than 0? I have no idea what these mean and can go read on it right now but I humbly suggest you would add a parenthesis-level description of these two concepts for future readers... – JohnDoea Nov 17 '16 at 3:55
  • 1
    @Benia stackoverflow.com/questions/5256599/… – muru Nov 17 '16 at 4:00
  • @muru I have read there passionately and discovered how OS's tend to organize process in trees/tables/u's and attach each file entry in the process these integer file descriptors starting from 1. Yet, I still fail to make the association between the number 3 in the following context --- Why is it 3 and not 2 or 1 (if not 0)... I believe that a clarification that could help and any other newcomer to understand the answer right here on this page, without longer learning outside and then coming back. – JohnDoea Nov 22 '16 at 4:54
  • 2
    I think the story is: The "3" comes from that "3<<" allocates that file descriptor for piping the text to, and then the fd3 gets inherited to the child, who reads it. You could use any free number, say "8<<" and would then need to refer to /dev/fd/8 accordingly. – Ralph Rönnquist Nov 22 '16 at 5:08
  • I suggest to anyone which is new to Linux to read in the following session if something didn't make sense above, and then read here again: unix.stackexchange.com/questions/358022/… – JohnDoea Apr 11 '17 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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