0

I publish this question with an answer so to clarify to new users, why some heredoc syntax would deal with file descriptors; I myself had some trouble understanding it at first.

See for example:

bash << EOF0 

    command1
    << E0F1
        Proudly_Printed_With_Heredoc
    EOF1

E0F0

Why should it be something like:

bash /dev/fd/5 5<< 'EOF0'
    command1
    cat << EOF1 >> /etc/apache2/apache2.conf
        Proudly_Printed_With_Heredoc
    EOF1
EOF0
4

Your first example is missing cat it doesn't need file descriptors.

bash << EOF0 

command1
cat << E0F1
        Proudly_Printed_With_Heredoc
EOF1

E0F0

however file descriptors are needed if you want to interact with some of the commands instead of having them take their input from the heredoc.

0

One needs to have an introduction with the whole concept of "file descriptors" to understand this answer - What is a "file descriptor" and why we need it. If you already have such an introduction, skip to the answer below; don''t --- Please read my answer in this SE thread, and then comeback.

Answer:

In the second way I presented in the question, we pass an heredoc to Bash on a file descriptor other than 0 (in this case - 5:

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

Notes:

  1. The heredoc hierarchy has nothing to do with the number of the relevant file descriptor.
  2. I gave the number 5 just because it's a nice number far from zero. AFAIK, you could give 50, or 500, or 50,000, and it would work the same way.

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.