1 .\" $OpenBSD: script.7,v 1.6 2008/09/19 05:41:29 djm Exp $
3 .\" $NetBSD: script.7,v 1.1 2005/05/07 02:20:34 perry Exp $
5 .\" Copyright (c) 2005 The NetBSD Foundation, Inc.
6 .\" All rights reserved.
8 .\" This document was originally contributed to The NetBSD Foundation
9 .\" by Perry E. Metzger of Metzger, Dowdeswell & Co. LLC.
11 .\" Redistribution and use in source and binary forms, with or without
12 .\" modification, are permitted provided that the following conditions
14 .\" 1. Redistributions of source code must retain the above copyright
15 .\" notice, this list of conditions and the following disclaimer.
16 .\" 2. Redistributions in binary form must reproduce the above copyright
17 .\" notice, this list of conditions and the following disclaimer in the
18 .\" documentation and/or other materials provided with the distribution.
20 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 .\" POSSIBILITY OF SUCH DAMAGE.
32 .Dd $Mdocdate: September 19 2008 $
37 .Nd interpreter script execution
39 The system is capable of treating a text file containing commands
40 intended for an interpreter, such as
44 as an executable program.
47 .Dq interpreter script
48 is a file which has been set executable (see
50 and which has a first line of the form:
52 .D1 Li #! Ar pathname Op Ar argument
56 must appear as the first two characters of the file.
66 and the length of the entire line is limited (see below).
68 If such a file is executed (such as via the
70 system call), the interpreter specified by the
72 is executed by the system.
75 is executed without regard to the
77 variable, so in general
79 should be an absolute path.)
81 The arguments passed to the interpreter will be as follows.
83 will be the path to the interpreter itself, as specified on the first
89 on the first line of the script, it will be passed as
91 The subsequent elements of
93 will be the path to the interpreter script file itself (i.e. the
96 followed by any further arguments passed when
98 was invoked to execute the script file.
100 By convention, it is expected that an interpreter will open the script
101 file passed as an argument and process the commands within it.
102 Typical interpreters treat
104 as a comment character, and thus will ignore the initial line of the script
107 but there is no requirement for this per se.
111 the length of the interpreter line following the
117 Other operating systems impose different limits on the length of
122 Note that the interpreter may not itself be an interpreter script.
125 does not point to an executable binary, execution of the interpreter
127 .Ss Trampolines and Portable Scripts
128 Different operating systems often have interpreters located in
129 different locations, and the kernel executes the passed interpreter
130 without regard to the setting of environment variables such as
132 This makes it somewhat challenging to set the
134 line of a script so that it will run identically on different systems.
138 utility executes a command passed to it on its command line, it is
141 to render scripts portable.
142 If the leading line of a script reads
144 .Dl #! /usr/bin/env interp
148 command will execute the
150 command it finds in its
152 passing on to it all subsequent arguments with which it itself was called.
155 is found on almost all
157 style systems, this trick is frequently exploited by authors who need
158 a script to execute without change on multiple systems.
159 .Ss Historical Note: Scripts without `#!'
160 Shell scripts predate the invention of the
162 convention, which is implemented in the kernel.
165 there was only one interpreter used on the system,
167 and the shell treated any file that failed to execute with an
176 and certain other facilities (including
180 but not other types of
183 interpreter scripts that do not include the
185 (and thus fail to execute with
190 As this behavior is implemented outside the kernel, there is no
191 mechanism that forces it to be respected by all programs that execute
193 It is thus not completely reliable.
194 It is therefore important to always include
198 in front of Bourne shell scripts, and to treat the traditional
199 behavior as obsolete.
201 Suppose that an executable binary exists in
206 .Bd -literal -offset indent
218 .Dl $ /tmp/script one two three
220 at the shell will result in
222 being executed, receiving the following arguments in
225 .Bd -ragged -offset indent
233 .Ss Portability Note: Multiple arguments
234 The behavior of multiple arguments on the
236 line is highly non-portable between different systems.
237 In general, only one argument can be assumed to work consistently.
239 Consider the following variation on the previous example.
240 Suppose that an executable binary exists in
245 .Bd -literal -offset indent
257 .Dl $ /tmp/script one two three
259 at the shell will result in
261 being executed, receiving the following arguments in
264 .Bd -ragged -offset indent
277 as a single argument.
281 style operating systems will pass only one
283 the behavior when multiple arguments are included is not
284 consistent between platforms.
287 will concatenate multiple arguments into a single argument (as above),
288 some will truncate them, and at least one will pass them as multiple
293 behavior is common but not universal.
296 would present the above argument as
301 Perhaps uniquely, recent versions of Apple's
303 will actually pass multiple arguments properly, i.e.:
304 .Bd -ragged -offset indent
314 The behavior of the system in the face of multiple arguments is thus
315 not currently standardized, should not be relied on, and may be
316 changed in future releases.
317 In general, pass at most one argument, and do not rely on multiple
318 arguments being concatenated.
330 The behavior of interpreter scripts is obliquely referred to, but
331 never actually described in,
334 The behavior is partially (but not completely) described in the
337 Although it has never been formally standardized, the behavior
338 described is largely portable across
340 style systems, with two significant exceptions: the maximum length of the
342 line, and the behavior if multiple arguments are passed.
343 Please be aware that the behavior in the
344 face of multiple arguments is not consistent across systems.
346 The behavior of the kernel when encountering scripts that start in
350 A Usenet posting to net.unix by Guy Harris on October 16, 1984 claims
351 that the idea for the
353 behavior was first proposed by Dennis Ritchie but that the first
354 implementation was on
357 Historical manuals (specifically the exec man page) indicate that the
358 behavior was present in
360 at least as early as April, 1981.
361 Information on precisely when it was first implemented, and in which
366 Numerous security problems are associated with setuid interpreter
369 In addition to the fact that many interpreters (and scripts) are
370 simply not designed to be robust in a setuid context, a race condition
371 exists between the moment that the kernel examines the interpreter
372 script file and the moment that the newly invoked interpreter opens
375 Subtle techniques can be used to subvert even seemingly well written scripts.
376 Scripts executed by Bourne type shells can be subverted in numerous
377 ways, such as by setting the
379 variable before executing the script.
380 Other interpreters possess their own vulnerabilities.
381 Setting the Set-user-ID on execution (SUID) bit
382 is therefore very dangerous, and should not be done lightly, if at all.