I wanted to see how some ASCII art looked in terminal so:$ cat <<EOF                                                                                                                                                                                  > #          ____         _> #  _   _  / __/  ___ _ | |_> # | | | |/ /    /  _` || __|> # | |_| |\ \__  (_| || |_> # |  _,_| \___\ \___,_| \__|> # |_/> #> EOFbash: bad substitution: no closing ` in ` || __|# | |_| |\ \__  (_| || |_# |  _,_| \___\ \___,_| \__|# |_/#The # octothorpes were there perchance, but now I'm confused.$ cat <<EOF> # echo hi> EOF# echo hiAs expected.However:$ cat <<EOF> # `echo hello`> EOF# helloSo bash gets at expanding `` and $( ) before cat does, but it doesn't care about # comments? What's the explanation behind this behaviour?
Why do here-documents attempt shell substitution even on a commented line?
bash;cat;variable substitution
This is more general than bash.  In POSIX shell, your EOF is referred to as a word, in the discussion of here-documents:If no characters in word are quoted, all lines of the here-document shall be expanded for parameter expansion, command substitution, and arithmetic expansion. In this case, the <backslash> in the input behaves as the <backslash> inside double-quotes (see Double-Quotes). However, the double-quote character ( '' ) shall not be treated specially within a here-document, except when the double-quote appears within $(), ``, or ${}.Quoting is done using single-, double-quotes or the backslash character.  POSIX mentions the here-documents in the discussion of quoting:The various quoting mechanisms are the escape character, single-quotes, and double-quotes. The here-document represents another form of quoting; see Here-Document.The key to understanding the lack of treatment of # characters is the definition for here-documents:allow redirection of lines contained in a shell input fileThat is, no meaning (other than possible parameter expansion, etc) is given to the data by the shell, because the data is redirected to another program: cat, which is not a shell interpreter.  If you redirected to a shell program, the result would be whatever the shell could do with the data.