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?

  • I wonder what you mean by "double ampersands"?
    – Kusalananda
    Dec 31, 2016 at 12:36
  • Hi, I mean to &&.
    – user149572
    Dec 31, 2016 at 12:42
  • Ah, I was confused. My brain thought "asterisks".
    – Kusalananda
    Dec 31, 2016 at 12:44

1 Answer 1


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
# contents of script
# goes here

Such 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.sh


$ 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.

  • Hi, you say "Yes, in this case". Can you clarify what particular case you mean to, or you mean globally ?...
    – user149572
    Dec 31, 2016 at 12:43
  • I mean that in this case you're not running the script (in the here-doc) as an executable from the command line. You are in fact specifically saying "run this with bash". So, in this case, the shebang will just be a comment in the script. But, as I wrote, if the script in the here-doc was written to a file (cat >myscript.sh <<END_OF_SCRIPT), then the shebang would be needed, and the script would need to be made executable. It all comes down to how you plan to run the script in the here-doc.
    – Kusalananda
    Dec 31, 2016 at 12:47
  • I thank you dearly for the answer and detailing!
    – user149572
    Dec 31, 2016 at 12:57
  • 1
    "The hash-bang line is used when executing an executable text file from the shell. The line tells the shell what interpreter to use to execute" => No, the shell does not analyze the file. The first line is read by the kernel when the script is executed as a stand-alone executable (i.e. ./my_script.sh vs /bin/shell my_script.sh), whatever executes it (not only the shell but any call to the execve() system call).
    – xhienne
    Dec 31, 2016 at 15:46
  • 1
    I'm a proud nitpicker. Thanks and happy new year @Kusalananda
    – xhienne
    Dec 31, 2016 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy