Shebang (Unix)
In computing, a shebang is the character sequence , consisting of the characters number sign (also known as hash or octothorpe) and exclamation mark (also known as bang), at the beginning of a script. 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 were an executable in a Unix-like operating system, the program loader mechanism parses the rest of the file's initial line as an interpreter directive. 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... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
In computing, a shebang is the character sequence , consisting of the characters number sign (also known as hash or octothorpe) and exclamation mark (also known as bang), at the beginning of a script. 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 were an executable in a Unix-like operating system, the program loader mechanism parses the rest of the file's initial line as an interpreter directive. 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 line #!/bin/sh , then the program loader is instructed to run the program /bin/sh, passing path/to/script as the first argument. 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. ==Syntax== The form of a shebang interpreter directive is as follows: #!interpreter [optional-one-arg-only] in which interpreter is a path to an executable program. It is optional to have a space between and interpreter. There could be any number of spaces or tabs either before or after interpreter. The optional-arg will include any extra spaces up to the end-of-line. In Linux, the file specified by interpreter can be executed if it has the execute rights and is one of the following: a native executable, such as an ELF binary any kind of file for which an interpreter was registered via the binfmt_misc mechanism (such as for executing Microsoft .exe binaries using wine) another script starting with 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 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), the file specified by interpreter must be an executable binary and cannot itself be a script. If the script is invoked by explicitly calling the interpreter, the #! line is never consulted. For example, if script.sh ’s first line is #!/usr/games/nibbles , running sh script.sh will not try to open the script in nibbles , but ./script.sh will. ==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 #!/usr/bin/pwsh – Execute the file using PowerShell #!/usr/bin/env python3 – Execute with a Python interpreter, using the env program search path to find it #!/bin/false – Do nothing, but return a non-zero exit status, 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. For example, consider a script having the initial line #!/bin/sh -x . It may be invoked simply by giving its file path, such as some/path/to/foo , and some parameters, such as bar and baz : some/path/to/foo bar 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 /bin/sh -x some/path/to/foo bar baz Most interpreters make any additional arguments available to the script. If /bin/sh is a POSIX-compatible shell, then bar and baz are presented to the script as the positional parameter array "$@" , and individually as parameters "$1" and "$2" respectively. Because the initial # is the character used to introduce comments in the POSIX 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, and not all do so; thus, a script consisting of the following two lines simply outputs both lines 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 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. 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== ===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 of . This magic number is detected by the "exec" 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, 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. However, UTF-8 files may begin with the optional byte order mark (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 (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 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 of SHArp bang 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 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 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 call would fail, preventing scripts from behaving uniformly as normal system commands. From: "Ritchie, Dennis M (Dennis)** CTR **" To: 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 File association URI fragment ==References== ==External links== Details about the shebang mechanism on various Unix flavours #! - the Unix truth as far as I know it (a more generic approach) FOLDOC shebang article
Cite this
"Shebang (Unix)". 1790319669. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1376631696 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy