MarketShebang (Unix)
Company Profile

Shebang (Unix)

In computing, a shebang is the character sequence #!, consisting of the characters number sign and exclamation mark, at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling.

Syntax
The form of a shebang interpreter directive is as follows: 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==
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==
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==
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, 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, 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 systems, /bin/sh is a hard or symbolic link to /bin/bash, the Bourne Again shell (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 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 does not standardize path names, POSIX does not standardize the feature. The GNU Autoconf 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. 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, typically . This particular example (using ) is of limited utility: neither nor is universal, with similar numbers of devices lacking each. More broadly using for any script still has some portability issues with OpenServer 5.0.6 and Unicos 9.0.2 which have only and no . Using results in run-time indirection, which has the potential to degrade system security; for this reason some commentators recommend against its use in packaged software, reserving it only for "educational examples". Argument splitting Command arguments are split in different ways across platforms. Some systems do not split up the arguments; for example, when running the script with the first line, #!/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. Such systems include Linux and Cygwin. Another approach is the use of a 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 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. Character interpretation Another problem is scripts containing a carriage return 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. 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 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; ==Etymology==
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==
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. Version 8 improved shell scripts 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, 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. Unnamed shell script feature The feature's creator didn't give it a name, however: 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==
tickerdossier.comtickerdossier.substack.com