6

Is the Shebang #!, e.g.

#!/bin/sh

in front of script executables officially standardized in the Linux Standard Base or in any of The Open Group standards or elsewhere? If yes, please provide references and details.

NOTE: I'm most interested in its meaning for shell scripts as well as for any executable file. In other words, do any of the standards require shebang-like interpretation of #! at the beginning of executable files? However, any other references to it in the standards are also welcome.

jotik
  • 17,044
  • 13
  • 58
  • 123

2 Answers2

7

POSIX leaves the effect of #! unspecified. From 2.1 Shell Introduction

The shell reads its input from a file (see sh), from the -c option or from the system() and popen() functions defined in the System Interfaces volume of POSIX.1-2008. If the first line of a file of shell commands starts with the characters "#!", the results are unspecified.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • 3
    The citation is about the shell command language. The quoted section is saying that the results of `sh foo` are unspecified if `foo` has a shebang (that is, if its first line begins with #!), but it says nothing about executing `./foo` directly. – tomjakubowski Nov 08 '19 at 07:02
2

From the source:

Bash scripts often begin with #! /bin/bash (assuming that Bash has been installed in /bin), since this ensures that Bash will be used to interpret the script, even if it is executed under another shell.

You can also check this The #! magic, details about the shebang/hash-bang mechanism on various Unix flavours

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331