27.11.2014 Views

INTRO (7) NetBSD Miscellaneous Information Manual INTRO (7 ...

INTRO (7) NetBSD Miscellaneous Information Manual INTRO (7 ...

INTRO (7) NetBSD Miscellaneous Information Manual INTRO (7 ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

SCRIPT (7) <strong>NetBSD</strong> <strong>Miscellaneous</strong> <strong>Information</strong> <strong>Manual</strong> SCRIPT (7)<br />

NAME<br />

script —interpreter script execution<br />

DESCRIPTION<br />

The system is capable of treating a text file containing commands intended for an interpreter, such as sh(1)<br />

or awk(1), as an executable program.<br />

An “interpreter script” is a file which has been set executable (see chmod(2)) and which has a first line of the<br />

form:<br />

#! pathname [argument]<br />

The “#!” must appear as the first two characters of the file. Aspace between the “#!” and pathname is<br />

optional. At most one argument may follow pathname, and the length of the entire line is limited (see<br />

below).<br />

If such a file is executed (such as via the execve(2) system call), the interpreter specified by the<br />

pathname is executed by the system. (The pathname is executed without regard to the PATH variable, so<br />

in general pathname should be an absolute path.)<br />

The arguments passed to the interpreter will be as follows. argv[0] will be the path to the interpreter itself,<br />

as specified on the first line of the script. If there is an argument following pathname on the first line of<br />

the script, it will be passed as argv[1]. The subsequent elements of argv will be the path to the interpreter<br />

script file itself (i.e. the original argv[0]) followed by any further arguments passed when execve(2) was<br />

invoked toexecute the script file.<br />

By convention, it is expected that an interpreter will open the script file passed as an argument and process<br />

the commands within it. Typical interpreters treat ‘#’ as a comment character, and thus will ignore the initial<br />

line of the script because it begins “#!”, but there is no requirement for this per se.<br />

On <strong>NetBSD</strong>, the length of the “#!” line, excluding the “#!” itself, is limited to PATH_MAX (as defined in<br />

〈limits.h〉). Other operating systems impose much smaller limits on the length of the “#!” line (see<br />

below).<br />

Note that the interpreter may not itself be an interpreter script. If pathname does not point to an executable<br />

binary, execution of the interpreter script will fail.<br />

Trampolines and Portable Scripts<br />

Different operating systems often have interpreters located in different locations, and the kernel executes the<br />

passed interpreter without regard to the setting of environment variables such as PATH. This makes it somewhat<br />

challenging to set the “#!” line of a script so that it will run identically on different systems.<br />

Since the env(1) utility executes a command passed to it on its command line, it is often used as a<br />

“trampoline” to render scripts portable. If the leading line of a script reads<br />

#! /usr/bin/env interp<br />

then the env(1) command will execute the “interp” command it finds in its PATH, passing on to it all subsequent<br />

arguments with which it itself was called. Since /usr/bin/env is found on almost all POSIX style<br />

systems, this trick is frequently exploited by authors who need a script to execute without change on multiple<br />

systems.<br />

Historical Note: Scripts without “#!”<br />

Shell scripts predate the invention of the “#!” convention, which is implemented in the kernel. In the days of<br />

Version 7 AT&T UNIX, there was only one interpreter used on the system, /bin/sh, and the shell treated<br />

any file that failed to execute with an ENOEXEC error (see intro(2)) as a shell script.<br />

<strong>NetBSD</strong> 3.0 May 6, 2005 1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!