I have a case of twisted shell logic to submit to you, since I have found nothing on that particular point anywhere (please accept my apologies if I missed it).I'll try to explain the context as best I can.I have a first scriptA which I expect to generate a second scriptB through the use of a heredoc. There are two items I want to have in this heredoc: a my_source file include, and some variables local to scriptA (for expansion within scriptA, that is). Here is an example:scriptA:#!/bin/shlogfile=/path/to/my_logfilescriptB=/path/to/my_scriptcat > ${scriptB} << __EOF__  . /path/to/my_source  #this is a shell script  echo Some text | tee -a ${logfile}__EOF__My question on the snippet above is: will shell expansion on the . instruction (i.e. inclusion of my_source) occur in scriptA or in scriptB?Bonus: is it possible to specify somehow that some part of the here doc should not be expanded?Note: I tried to be POSIX-compliant here and avoid bash specificsThanks for the insights!EDIT: Answer is that the . utility shall not trigger in the here-doc in scriptA. This is because a here-doc only performs:parameter expansion, command substitution, and arithmetic expansionThe . is a shell special built-in utility, which is not expanded (just like all shell built-ins, including bash-, ksh-, tcsh-specific (...) built-ins).Thanks @ilkkachu for the insight.
How will the shell expand an include in a here-doc?
shell script;posix;here document
No, a here-doc is expanded mostly like a double-quoted string:If no part of word is quoted, all lines of the here-document shall be expanded for parameter expansion, command substitution, and arithmetic expansion. Backslashes also work to escape special characters.The note about quoting means that something like <<EOF would prevent expanding anything within the here-doc. In your example, the reference to ${logfile} would be expanded by the shell before passing the input to cat.You could still use command substitution if you want to include the output of a command, but if all you want is that, using a pipe would also be a logical choice.