2

In some heredocs, you see the keyword bash before the opener (<<), the name, and the File descriptor. For example:

bash /dev/fd/10 10<<'SES'

Why is the Bash there? I mean, we already work in Bash, and it's not like cat used to display the text and then redirect it to another file as in cat << EOT >> /file.txt hence I ask as a freshman, why we need the keyword bash there.

  • Because if you're in some other environment (e. g. fish, tcsh, ksh, et cetera), you're not already in Bash, so you may need to explicitly invoke the shell in case you're currently in one that does not support heredocs. – DopeGhoti Mar 22 '17 at 16:38
3

bash there isn't a keyword, it's a command being executed. It's being passed the argument /dev/fd/10. The shell running the script (which may or may not be bash) is setting up a heredoc to be read from file descriptor 10.

So, bash is being invoked to run the heredoc as a shell script. It's similar to if the line read bash /path/to/script.sh — in this case, the script is just coming from a heredoc instead.

Why someone did that (instead of just using ( commands ) is impossible to answer without the context (and possibly the history as well). One guess (as DopeGhoti suggests) is that the script may be running under a different shell, and that bit requires bash. Another possibility is that the script's author just didn't know the alternate methods.

As to why FD 10 is used instead of stdin, it could be because the script block needs to read from stdin as well.

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.