Shebang (Unix)

The shebang line is usually ignored by the interpreter, because the "#" character is a comment marker in many scripting languages; some language interpreters that do not use the hash mark to begin comments still may ignore the shebang line in recognition of its purpose.

A chain of shebangs and wrappers yields a directly executable file that gets the encountered scripts as parameters in reverse order.

In Solaris- and Darwin-derived operating systems (such as macOS), the file specified by interpreter must be an executable binary and cannot itself be a script.

It may be invoked simply by giving its file path, such as some/path/to/foo, [12] and some parameters, such as bar and baz: In this case /bin/sh is invoked in its place, with parameters -x, some/path/to/foo, bar, and baz, as if the original command had been Most interpreters make any additional arguments available to the script.

It also allows specific selection of interpreter, without overloading the filename extension namespace (where one file extension refers to more than one file type), and allows the implementation language of a script to be changed without changing its invocation syntax by other programs.

[13] In many Linux systems, /bin/sh is a hard or symbolic link to /bin/bash, the Bourne Again shell (BASH).

This particular example (using sh) is of limited utility: neither /bin/sh nor /usr/bin/env is universal, with similar numbers of devices lacking each.

Using #!/usr/bin/env results in run-time indirection, which has the potential to degrade system security; for this reason some commentators recommend against its use[17] in packaged software, reserving it only for "educational examples".

FreeBSD 6.0 (2005) introduced a -S option to its env as it changed the shebang-reading behavior to non-splitting.

[21] Although using this option mitigates the portability issue on the kernel end with splitting, it adds the requirement that env supports this particular extension.

This magic number is detected by the "exec" family of functions, which determine whether a file is a script or an executable binary.

It has been claimed[23] that some old versions of Unix expect the normal shebang to be followed by a space and a slash (#!

/), but this appears to be untrue;[11] rather, blanks after the shebang have traditionally been allowed, and sometimes documented with a space, as described in the 1980 historical email below.

The shebang characters are represented by the same two bytes in extended ASCII encodings, including UTF-8, which is commonly used for scripts and other text files on current Unix-like systems.

Some authorities recommend against using the byte order mark in POSIX (Unix-like) scripts,[24] for this reason and for wider interoperability and philosophical concerns.

It was also added to the BSD releases from Berkeley's Computer Science Research (present at 2.8BSD[27] and activated by default by 4.2BSD).