Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Shebang (#!) standardized?

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.

like image 757
jotik Avatar asked May 18 '16 09:05

jotik


People also ask

What is the shebang used for?

What is the shebang? The shebang is a special character sequence in a script file that specifies which program should be called to run the script. The shebang is always on the first line of the file, and is composed of the characters #! followed by the path to the interpreter program.

Why is it called a shebang?

Among UNIX shell (user interface) users, a shebang is a term for the "#!" characters that must begin the first line of a script. In musical notation, a "#" is called a sharp and an exclamation point - "!" - is sometimes referred to as a bang. Thus, shebang becomes a shortening of sharp-bang.

What is the Shabang?

Definition of shebang : everything involved in what is under consideration —usually used in the phrase the whole shebang.

Is the shebang necessary?

Shebangs should never really be necessary on any platform. If a shell script lacks a shebang, the script will be interpreted by whatever shell binary was called to interpret it. This is usually the same shell binary as the shell from which you invoked the script.


2 Answers

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.

like image 131
Jens Avatar answered Dec 12 '22 15:12

Jens


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

like image 29
Rahul Tripathi Avatar answered Dec 12 '22 14:12

Rahul Tripathi