20.3. Executable Scripts

An executable script is an executable file of which the first two characters are #! as defined in the portable character set. In POSIX 1003.1-2008 (ISO/IEC 9945-2009), this construct is undefined, but reserved for implementations which wish to provide this functionality. LSB conforming implementations shall support executable scripts.

A successful call to a function of the exec family with an executable script as the first parameter shall result in a new process, where the process image started is that of the interpreter. The path name of the interpreter follows the #! characters.

If the executable script has a first line

#! interpreter [arg]

then interpreter shall be called with an argument array consisting of an unspecified zeroth argument, followed by arg (if present), followed by a path name for the script, followed by the arguments following the zeroth argument in the exec call of the script.

The interpreter shall not perform any operations on the first line of an executable script.

The first line of the executable script shall meet all of the following criteria otherwise the results are unspecified:

  1. Is of one of the forms:

    #!interpreter
    #! interpreter
    #!interpreter arg
    #! interpreter arg

  2. The interpreter argument is an absolute pathname of an executable file other than an executable script.

  3. Neither the interpreter argument nor the arg argument, if present, contain any quoting characters.

  4. Neither the interpreter argument nor the arg argument, if present, contain any whitespace characters.

  5. The length of the entire line is no longer than 80 bytes.

If the interpreter is required by this specification to be in a specfic named directory, a conforming application must use that path for interpreter, as implementations are not prohibited from having other, possibly non-conforming, versions of the same interpreter installed on the system. If the interpreter is a required command in this specification, but does not have a required path, the application should take special measures to insure the appropriate version is selected. If the interpreter is not a required command in this specification, the application must make appropriate provisions that the interpreter is available at the appropriate path.

Note: In case the path is not specified, it is recommended that an installation script for executable scripts use the standard PATH returned by a call to the getconf command with the argument PATH, combined with the command command to determine the location of a standard command.

For example to determine the location of the standard awk command:

PATH=`getconf PATH` command -v awk

The installation script should ensure that the returned pathname is an absolute pathname prior to use, since a shell builtin might be returned for some utilities.

Use of the common form #!/usr/bin/env interpreter is not recommended as the PATH will be unknown at execution time and an alternative version of interpreter might be selected.