Is it true to conclude that when using a bash here-doc like bash << HEREDOC, then always and without exceptions, shebang lines like #!/bin/bash -x are redundant?If I would have to bet, I would bet that yes, they will be redundant, and could only use us to organize the information, like a sign saying for new users the following set of commands was originally executed from a traditional script, don't run them in one line with double ampersands or another similar way.I wonder what the experts say - Is the bash shebang line really are totally redundant when using a here-doc?
Bash here-documents and shebang lines
shell script;here document;shebang
Yes, in this case.Summary: The hash-bang line (also called shebang and other things) is only ever needed if it's in an executable script that is run without explicitly specifying its interpreter (just as when executing a binary executable file).In the case of your code, you're explicitly running the script within the here-document with bash already. There's no need to add the hash-bang as it will be treated as a comment.Since the here-document script seems to want to be executed with the -x option set (judging from #!/bin/bash -x), you will have to use set -x inside the here-document to get the same effect as if running the here-document as its own script, again, because the hash-bang is treated as a comment.The hash-bang line is used when executing an executable text file. The line tells the system what interpreter to use to execute it, and optionally allows you to specify one argument to that interpreter (-x in your case).You do need the hash-bang there if you're writing the here-document to a file that will later be used as a script. For example:cat >myscript.sh <<<END_OF_SCRIPT#!/bin/bash# contents of script# goes hereEND_OF_SCRIPTSuch a file also has to be made executable (chmod +x myscript.sh). But again, if you were to explicitly execute that script with bash, for example through$ bash ./myscript.shor$ bash -x ./myscript.sh(or equivalently from within another script) then no hash-bang is needed, and the script would not have to be made executable.It all comes down to how you would want to execute the script.See also the Wikipedia entry for Shebang.