Search code examples
bashshellposixshebang

Why would someone use two shebangs in a shell script?


I have inherited a medium-sized collection of scripts where some of them start with two shebangs, like this:

#!/bin/sh
#!/bin/bash
[do stuff]

Is there a valid reason for using this construct?

In my experience, any Unix will only respect the first line as a shebang, and the second line will be the first line of the script, which the sh interpreter will ignore as being a comment. Should I assume this is a mistake by the programmer? Is there any difference in, say, compatibility or portability if I were to simply remove the second shebang?


Solution

  • You're right: there is no use for having two shebang lines. Only the first will ever be used.

    Perhaps it was a mistake during some previous automated refactoring. Just look in your revision control system--I'm absolutely certain whoever did this left a clear and detailed comment explaining their rationale.