HOME

TheInfoList



OR:

In
computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes, and development of both hardware and software. Computing has scientific, e ...
, a shebang is the character sequence consisting of the characters
number sign The symbol is known variously in English-speaking regions as the number sign, hash, or pound sign. The symbol has historically been used for a wide range of purposes including the designation of an ordinal number and as a Typographic ligature, ...
and
exclamation mark The exclamation mark, , or exclamation point (American English), is a punctuation mark usually used after an interjection or exclamation to indicate strong feelings or to show emphasis. The exclamation mark often marks the end of a sentence, f ...
() at the beginning of a
script Script may refer to: Writing systems * Script, a distinctive writing system, based on a repertoire of specific elements or symbols, or that repertoire * Script (styles of handwriting) ** Script typeface, a typeface with characteristics of handw ...
. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling. When a text file with a shebang is used as if it is an executable in a
Unix-like A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
operating system, the
program loader In computer systems a loader is the part of an operating system that is responsible for loading programs and libraries. It is one of the essential stages in the process of starting a program, as it places programs into memory and prepares them ...
mechanism parses the rest of the file's initial line as an
interpreter directive An interpreter directive is a computer language construct, that on some systems is better described as an aspect of the system's executable file format, that is used to control which interpreter parses and interprets the instructions in a compute ...
. The loader executes the specified interpreter program, passing to it as an argument the path that was initially used when attempting to run the script, so that the program may use the file as input data. For example, if a script is named with the path ''path/to/script'', and it starts with the following line, #!/bin/sh, then the program loader is instructed to run the program ''/bin/sh'', passing ''path/to/script'' as the first argument. In
Linux Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, which ...
, this behavior is the result of both kernel and user-space code. The shebang line is usually ignored by the interpreter, because the "#" character is a
comment Comment may refer to: * Comment (linguistics) or rheme, that which is said about the topic (theme) of a sentence * Bernard Comment (born 1960), Swiss writer and publisher Computing * Comment (computer programming), explanatory text or informa ...
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.


Syntax

The form of a shebang
interpreter directive An interpreter directive is a computer language construct, that on some systems is better described as an aspect of the system's executable file format, that is used to control which interpreter parses and interprets the instructions in a compute ...
is as follows: #!''interpreter'' 'optional-arg''in which ''interpreter'' is generally an absolute path to an executable program. The optional argument is a string representing a single argument. White space after is optional. In
Linux Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, which ...
, the file specified by ''interpreter'' can be executed if it has one of the following: * the execute right and contains code which the kernel can execute directly, * a wrapper defined for it via ''
sysctl sysctl is a software utility of some Unix-like operating systems that reads and modifies the attributes of the system kernel such as its version number, maximum limits, and security settings. It is available both as a system call for compiled ...
'' (such as for executing Microsoft
.exe .exe is a common filename extension denoting an executable file (the main execution point of a computer program) for Microsoft Windows, OS/2, and DOS. File formats There are numerous file formats which may be used by a file with a extensi ...
binaries using
wine Wine is an alcoholic drink typically made from fermented grapes. Yeast consumes the sugar in the grapes and converts it to ethanol and carbon dioxide, releasing heat in the process. Different varieties of grapes and strains of yeasts are m ...
), * a shebang. On Linux and Minix, an interpreter can also be a script. A chain of shebangs and wrappers yields a directly executable file that gets the encountered scripts as parameters in reverse order. For example, if file ''/bin/A'' is an executable file in
ELF An elf () is a type of humanoid supernatural being in Germanic mythology and folklore. Elves appear especially in North Germanic mythology. They are subsequently mentioned in Snorri Sturluson's Icelandic Prose Edda. He distinguishes "ligh ...
format, file ''/bin/B'' contains the shebang , and file ''/bin/C'' contains the shebang , then executing file ''/bin/C'' resolves to , which finally resolves to . In Solaris- and Darwin-derived operating systems (such as
macOS macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac computers. Within the market of desktop and lapt ...
), the file specified by ''interpreter'' must be an executable binary and cannot itself be a script.


Examples

Some typical shebang lines: * #!/bin/sh – Execute the file using the Bourne shell, or a compatible shell, assumed to be in the /bin directory * #!/bin/bash – Execute the file using the
Bash shell Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions. Bash was o ...
* #!/usr/bin/pwsh – Execute the file using
PowerShell PowerShell is a task automation and configuration management program from Microsoft, consisting of a command-line shell (computing), shell and the associated scripting language. Initially a Windows component only, known as Windows PowerShell, it ...
* #!/usr/bin/env python3 – Execute with a
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
interpreter, using the env program search path to find it * #!/bin/false – Do nothing, but return a non-zero
exit status The exit status of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task. In DOS, this may be referred ...
, indicating failure. Used to prevent stand-alone execution of a script file intended for execution in a specific context, such as by the . command from sh/bash, source from csh/tcsh, or as a .profile, .cshrc, or .login file. Shebang lines may include specific options that are passed to the interpreter. However, implementations vary in the parsing behavior of options; for portability, only one option should be specified without any embedded whitespace. Further portability guidelines are found below.


Purpose

Interpreter directives allow scripts and data files to be used as commands, hiding the details of their implementation from users and other programs, by removing the need to prefix scripts with their interpreter on the command line. A Bourne shell script that is identified by the path ''some/path/to/foo'', has the initial line, #!/bin/sh -x and is executed with parameters ''bar'' and ''baz'' as some/path/to/foo bar baz provides a similar result as having actually executed the following command line instead: /bin/sh -x some/path/to/foo bar baz If ''/bin/sh'' specifies the Bourne shell, then the end result is that all of the shell commands in the file ''some/path/to/foo'' are executed with the positional variables ''$1'' and ''$2'' having the values ''bar'' and ''baz'', respectively. Also, because the initial
number sign The symbol is known variously in English-speaking regions as the number sign, hash, or pound sign. The symbol has historically been used for a wide range of purposes including the designation of an ordinal number and as a Typographic ligature, ...
is the character used to introduce comments in the Bourne shell language (and in the languages understood by many other interpreters), the whole shebang line is ignored by the interpreter. However, it is up to the interpreter to ignore the shebang line; thus, a script consisting of the following two lines simply echos ''both'' lines to
standard output In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin ...
when run: #!/bin/cat Hello world!


Strengths

When compared to the use of global association lists between file extensions and the interpreting applications, the interpreter directive method allows users to use interpreters not known at a global system level, and without administrator rights. It also allows specific selection of interpreter, without overloading the
filename extension A filename extension, file name extension or file extension is a suffix to the name of a computer file (e.g., .txt, .docx, .md). The extension indicates a characteristic of the file contents or its intended use. A filename extension is typically d ...
namespace In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified. Namespaces ...
(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. Invokers of the script need not know what the implementation language is as the script itself is responsible for specifying the interpreter to use.


Portability


Program location

Shebangs must specify absolute paths (or paths relative to current working directory) to system executables; this can cause problems on systems that have a non-standard file system layout. Even when systems have fairly standard paths, it is quite possible for variants of the same operating system to have different locations for the desired interpreter.
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
, for example, might be in ''/usr/bin/python3'', ''/usr/local/bin/python3'', or even something like ''/home/username/bin/python3'' if installed by an ordinary user. A similar problem exists for the
POSIX shell A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to ...
, since POSIX only required its name to be ''sh'', but did not mandate a path. A common value is , but some systems such as Solaris have the POSIX-compatible shell at ''/usr/xpg4/bin/sh''. In many
Linux Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, which ...
systems, ''/bin/sh'' is a hard or
symbolic link In computing, a symbolic link (also symlink or soft link) is a file whose purpose is to point to a file or directory (called the "target") by specifying a path thereto. Symbolic links are supported by POSIX and by most Unix-like operating syste ...
to ''/bin/bash'', the
Bourne Again shell Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions. Bash was o ...
(BASH). Using bash-specific syntax while maintaining a shebang pointing to ''sh'' is also not portable. Because of this it is sometimes required to edit the shebang line after copying a
script Script may refer to: Writing systems * Script, a distinctive writing system, based on a repertoire of specific elements or symbols, or that repertoire * Script (styles of handwriting) ** Script typeface, a typeface with characteristics of handw ...
from one computer to another because the path that was coded into the script may not apply on a new machine, depending on the consistency in past convention of placement of the interpreter. For this reason and because
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming interf ...
does not standardize path names, POSIX does not standardize the feature. The
GNU GNU () is an extensive collection of free software (383 packages as of January 2022), which can be used as an operating system or can be used in parts with other operating systems. The use of the completed GNU tools led to the family of operat ...
Autoconf GNU Autoconf is a tool for producing configure scripts for building, installing, and packaging software on computer systems where a Bourne shell is available. Autoconf is agnostic about the programming languages used, but it is often used for ...
tool can test for system support with the macro AC_SYS_INTERPRETER. Often, the program can be used to circumvent this limitation by introducing a level of
indirection In computer programming, indirection (also called dereferencing) is the ability to reference something using a name, reference, or container instead of the value itself. The most common form of indirection is the act of manipulating a value throug ...
. is followed by , followed by the desired command without full path, as in this example: #!/usr/bin/env sh This mostly works because the path is commonly used for the utility, and it invokes the first found in the user's
$PATH PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting. ...
, typically . This still has some portability issues with
OpenServer Xinuos OpenServer, previously SCO UNIX and SCO Open Desktop (SCO ODT), is a closed source computer operating system developed by Santa Cruz Operation (SCO), later acquired by SCO Group, and now owned by Xinuos. Early versions of OpenServer wer ...
5.0.6 and
Unicos UNICOS is a range of Unix and after it Linux operating system (OS) variants developed by Cray for its supercomputers. UNICOS is the successor of the Cray Operating System (COS). It provides network clustering and source code compatibility layer ...
9.0.2 which have only and no .


Character interpretation

Another portability problem is the interpretation of the command arguments. Some systems, including Linux, do not split up the arguments; for example, when running the script with the first line like, #!/usr/bin/env python3 -c all text after the first space is treated as a single argument, that is, will be passed as one argument to , rather than two arguments. Cygwin also behaves this way. Complex interpreter invocations are possible through the use of an additional wrapper. FreeBSD 6.0 (2005) introduced a option to its as it changed the shebang-reading behavior to non-splitting. This option tells to split the string itself. The GNU utility since
coreutils The GNU Core Utilities or coreutils is a package of GNU software containing implementations for many of the basic tools, such as cat, ls, and rm, which are used on Unix-like operating systems. In September 2002, the ''GNU coreutils'' were cr ...
8.30 (2018) also includes this feature. Although using this option mitigates the portability issue on the kernel end with splitting, it adds the requirement that supports this particular extension. Another problem is scripts containing a
carriage return A carriage return, sometimes known as a cartridge return and often shortened to CR, or return, is a control character or mechanism used to reset a device's position to the beginning of a line of text. It is closely associated with the line feed ...
character immediately after the shebang line, perhaps as a result of being edited on a system that uses DOS line breaks, such as
Microsoft Windows Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
. Some systems interpret the carriage return character as part of the interpreter command, resulting in an error message.


Magic number

The shebang is actually a human-readable instance of a magic number in the executable file, the magic byte string being , the two-character encoding in
ASCII ASCII ( ), abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because of ...
of . This magic number is detected by the "
exec Exec or EXEC may refer to: * Executive officer, a person responsible for running an organization * Executive producer, provides finance and guidance for the making of a commercial entertainment product * A family of kit helicopters produced by Rot ...
" family of functions, which determine whether a file is a script or an executable binary. The presence of the shebang will result in the execution of the specified executable, usually an interpreter for the script's language. It has been claimed 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; rather, blanks after the shebang have traditionally been allowed, and sometimes documented with a space (see the 1980 email in
history History (derived ) is the systematic study and the documentation of the human activity. The time period of event before the History of writing#Inventions of writing, invention of writing systems is considered prehistory. "History" is an umbr ...
section below). The shebang characters are represented by the same two bytes in extended ASCII encodings, including
UTF-8 UTF-8 is a variable-width encoding, variable-length character encoding used for electronic communication. Defined by the Unicode Standard, the name is derived from ''Unicode'' (or ''Universal Coded Character Set'') ''Transformation Format 8-bit'' ...
, which is commonly used for scripts and other text files on current Unix-like systems. However, UTF-8 files may begin with the optional
byte order mark The byte order mark (BOM) is a particular usage of the special Unicode character, , whose appearance as a magic number at the start of a text stream can signal several things to a program reading the text: * The byte order, or endianness, of t ...
(BOM); if the "exec" function specifically detects the bytes and , then the presence of the BOM () before the shebang will prevent the script interpreter from being executed. Some authorities recommend against using the byte order mark in
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming interf ...
(Unix-like) scripts, for this reason and for wider interoperability and philosophical concerns. Additionally, a byte order mark is not necessary in UTF-8, as that encoding does not have
endianness In computing, endianness, also known as byte sex, is the order or sequence of bytes of a word of digital data in computer memory. Endianness is primarily expressed as big-endian (BE) or little-endian (LE). A big-endian system stores the mos ...
issues; it serves only to identify the encoding as UTF-8.


Etymology

An executable file starting with an interpreter directive is simply called a script, often prefaced with the name or general classification of the intended interpreter. The name ''shebang'' for the distinctive two characters may have come from an inexact
contraction Contraction may refer to: Linguistics * Contraction (grammar), a shortened word * Poetic contraction, omission of letters for poetic reasons * Elision, omission of sounds ** Syncope (phonology), omission of sounds in a word * Synalepha, merged ...
of ''
SHArp Sharp or SHARP may refer to: Acronyms * SHARP (helmet ratings) (Safety Helmet Assessment and Rating Programme), a British motorcycle helmet safety rating scheme * Self Help Addiction Recovery Program, a charitable organisation founded in 19 ...
bang Bang or bangs may refer to: Products * M1922 Bang rifle, a US semi-automatic rifle designed by Søren Hansen Bang * Bang, a model car brand * Bang (beverage), an energy drink Geography * Bang, Lorestan, a village in Iran * Bangs, Ohio, Uni ...
'' or ''haSH bang'', referring to the two typical Unix names for them. Another theory on the ''sh'' in ''shebang'' is that it is from the default shell ''sh'', usually invoked with shebang. This usage was current by December 1989, and probably earlier.


History

The shebang was introduced by Dennis Ritchie between Edition 7 and 8 at Bell Laboratories. It was also added to the
BSD The Berkeley Software Distribution or Berkeley Standard Distribution (BSD) is a discontinued operating system based on Research Unix, developed and distributed by the Computer Systems Research Group (CSRG) at the University of California, Berk ...
releases from Berkeley's Computer Science Research (present at 2.8BSD and activated by default by 4.2BSD). As AT&T Bell Laboratories Edition 8 Unix, and later editions, were not released to the public, the first widely known appearance of this feature was on BSD. The lack of an interpreter directive, but support for shell scripts, is apparent in the documentation from
Version 7 Unix Seventh Edition Unix, also called Version 7 Unix, Version 7 or just V7, was an important early release of the Unix operating system. V7, released in 1979, was the last Bell Laboratories release to see widespread distribution before the commercial ...
in 1979, which describes instead a facility of the Bourne shell where files with execute permission would be handled specially by the shell, which would (sometimes depending on initial characters in the script, such as ":" or "#") spawn a subshell which would interpret and run the commands contained in the file. In this model, scripts would only behave as other commands if called from within a Bourne shell. An attempt to directly execute such a file via the operating system's own ''exec()'' system trap would fail, preventing scripts from behaving uniformly as normal system commands. In later versions of Unix-like systems, this inconsistency was removed. Dennis Ritchie introduced kernel support for interpreter directives in January 1980, for
Version 8 Unix The term "Research Unix" refers to early versions of the Unix operating system for DEC PDP-7, PDP-11, VAX and Interdata 7/32 and 8/32 computers, developed in the Bell Labs Computing Sciences Research Center (CSRC). History The term ''Resear ...
, with the following description: From uucp Thu Jan 10 01:37:58 1980 >From dmr Thu Jan 10 04:25:49 1980 remote from research The system has been changed so that if a file being executed begins with the magic characters #! , the rest of the line is understood to be the name of an interpreter for the executed file. Previously (and in fact still) the shell did much of this job; it automatically executed itself on a text file with executable mode when the text file's name was typed as a command. Putting the facility into the system gives the following benefits. 1) It makes shell scripts more like real executable files, because they can be the subject of 'exec.' 2) If you do a 'ps' while such a command is running, its real name appears instead of 'sh'. Likewise, accounting is done on the basis of the real name. 3) Shell scripts can be set-user-ID. 4) It is simpler to have alternate shells available; e.g. if you like the Berkeley csh there is no question about which shell is to interpret a file. 5) It will allow other interpreters to fit in more smoothly. To take advantage of this wonderful opportunity, put #! /bin/sh at the left margin of the first line of your shell scripts. Blanks after ! are OK. Use a complete pathname (no search is done). At the moment the whole line is restricted to 16 characters but this limit will be raised. The feature's creator didn't give it a name, however: From: "Ritchie, Dennis M (Dennis)** CTR **" edacted To: < edactedtalisman.org> Date: Thu, 19 Nov 2009 18:37:37 -0600 Subject: RE: What do -you- call your #! line? I can't recall that we ever gave it a proper name. It was pretty late that it went in--I think that I got the idea from someone at one of the UCB conferences on Berkeley Unix; I may have been one of the first to actually install it, but it was an idea that I got from elsewhere. As for the name: probably something descriptive like "hash-bang" though this has a specifically British flavor, but in any event I don't recall particularly using a pet name for the construction. Kernel support for interpreter directives spread to other versions of Unix, and one modern implementation can be seen in the Linux kernel source in ''fs/binfmt_script.c''. This mechanism allows scripts to be used in virtually any context normal compiled programs can be, including as full system programs, and even as interpreters of other scripts. As a caveat, though, some early versions of kernel support limited the length of the interpreter directive to roughly 32 characters (just 16 in its first implementation), would fail to split the interpreter name from any parameters in the directive, or had other quirks. Additionally, some modern systems allow the entire mechanism to be constrained or disabled for security purposes (for example, set-user-id support has been disabled for scripts on many systems). Note that, even in systems with full kernel support for the ''#!'' magic number, some scripts lacking interpreter directives (although usually still requiring execute permission) are still runnable by virtue of the legacy script handling of the Bourne shell, still present in many of its modern descendants. Scripts are then interpreted by the user's default shell.


See also

* binfmt_misc *
CrunchBang Linux CrunchBang Linux (abbreviated #!) was a Linux distribution derived from Debian by Philip Newborough (who is more commonly known by his username, corenominal). CrunchBang was designed to use comparatively few system resources. Instead of a deskt ...
*
File association In computing, a file association associates a file with an application capable of opening that file. More commonly, a file association associates a class of files (usually determined by their filename extension, such as .txt) with a corresponding a ...
*
URI fragment In computer hypertext, a URI fragment is a string of characters that refers to a resource that is subordinate to another, primary resource. The primary resource is identified by a Uniform Resource Identifier (URI), and the fragment identifier po ...


Notes


References

{{Reflist, 30em


External links


Details about the shebang mechanism on various Unix flavours


(a more generic approach)
FOLDOC shebang article
Unix