xref: /freebsd/usr.bin/env/env.1 (revision 9517e866259191fcd39434a97ad849a9b59b9b9f)
1.\" Copyright (c) 1980, 1990, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the Institute of Electrical and Electronics Engineers, Inc.
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" From @(#)printenv.1	8.1 (Berkeley) 6/6/93
35.\" From FreeBSD: src/usr.bin/printenv/printenv.1,v 1.17 2002/11/26 17:33:35 ru Exp
36.\" $FreeBSD$
37.\"
38.Dd April 17, 2008
39.Dt ENV 1
40.Os
41.Sh NAME
42.Nm env
43.Nd set environment and execute command, or print environment
44.Sh SYNOPSIS
45.Nm
46.Op Fl iv
47.Op Fl P Ar altpath
48.Op Fl S Ar string
49.Op Fl u Ar name
50.Op Ar name Ns = Ns Ar value ...
51.Op Ar utility Op Ar argument ...
52.Sh DESCRIPTION
53The
54.Nm
55utility executes another
56.Ar utility
57after modifying the environment as
58specified on the command line.
59Each
60.Ar name Ns = Ns Ar value
61option specifies the setting of an environment variable,
62.Ar name ,
63with a value of
64.Ar value .
65All such environment variables are set before the
66.Ar utility
67is executed.
68.Pp
69The options are as follows:
70.Bl -tag -width indent
71.It Fl i
72Execute the
73.Ar utility
74with only those environment variables specified by
75.Ar name Ns = Ns Ar value
76options.
77The environment inherited
78by
79.Nm
80is ignored completely.
81.\"	-P
82.It Fl P Ar altpath
83Search the set of directories as specified by
84.Ar altpath
85to locate the specified
86.Ar utility
87program, instead of using the value of the
88.Ev PATH
89environment variable.
90.\"	-S
91.It Fl S Ar string
92Split apart the given
93.Ar string
94into multiple strings, and process each of the resulting strings
95as separate arguments to the
96.Nm
97utility.
98The
99.Fl S
100option recognizes some special character escape sequences and
101also supports environment-variable substitution, as described
102below.
103.\"	-u
104.It Fl u Ar name
105If the environment variable
106.Ar name
107is in the environment, then remove it before processing the
108remaining options.
109This is similar to the
110.Ic unset
111command in
112.Xr sh 1 .
113The value for
114.Ar name
115must not include the
116.Ql =
117character.
118.\"	-v
119.It Fl v
120Print verbose information for each step of processing done by the
121.Nm
122utility.
123Additional information will be printed if
124.Fl v
125is specified multiple times.
126.El
127.Pp
128The above options are only recognized when they are specified
129before any
130.Ar name Ns = Ns Ar value
131options.
132.Pp
133If no
134.Ar utility
135is specified,
136.Nm
137prints out the names and values
138of the variables in the environment, with one name/value pair per line.
139.\"
140.Ss Details of Fl S Ss (split-string) processing
141The processing of the
142.Fl S
143option will split the given
144.Ar string
145into separate arguments based on any space or <tab> characters found in the
146.Ar string .
147Each of those new arguments will then be treated as if it had been
148specified as a separate argument on the original
149.Nm
150command.
151.Pp
152Spaces and tabs may be embedded in one of those new arguments by using
153single
154.Pq Dq Li '
155or double
156.Pq Ql \&"
157quotes, or backslashes
158.Pq Ql \e .
159Single quotes will escape all non-single quote characters, up to
160the matching single quote.
161Double quotes will escape all non-double quote characters, up to
162the matching double quote.
163It is an error if the end of the
164.Ar string
165is reached before the matching quote character.
166.Pp
167If
168.Fl S
169would create a new argument that starts with the
170.Ql #
171character, then that argument and the remainder of the
172.Ar string
173will be ignored.
174The
175.Ql \e#
176sequence can be used when you want a new argument to start
177with a
178.Ql #
179character, without causing the remainder of the
180.Ar string
181to be skipped.
182.Pp
183While processing the
184.Ar string
185value,
186.Fl S
187processing will treat certain character combinations as escape
188sequences which represent some action to take.
189The character escape sequences are in backslash notation.
190The characters and their meanings are as follows:
191.Pp
192.Bl -tag -width indent -offset indent -compact
193.It Cm \ec
194Ignore the remaining characters in the
195.Ar string .
196This must not appear inside a double-quoted string.
197.It Cm \ef
198Replace with a <form-feed> character.
199.It Cm \en
200Replace with a <new-line> character.
201.It Cm \er
202Replace with a <carriage return> character.
203.It Cm \et
204Replace with a <tab> character.
205.It Cm \ev
206Replace with a <vertical tab> character.
207.It Cm \e#
208Replace with a
209.Ql #
210character.
211This would be useful when you need a
212.Ql #
213as the first character in one of the arguments created
214by splitting apart the given
215.Ar string .
216.It Cm \e$
217Replace with a
218.Ql $
219character.
220.It Cm \e_
221If this is found inside of a double-quoted string, then replace it
222with a single blank.
223If this is found outside of a quoted string, then treat this as the
224separator character between new arguments in the original
225.Ar string .
226.It Cm \e"
227Replace with a <double quote> character.
228.It Cm \e\'
229Replace with a <single quote> character.
230.It Cm \e\e
231Replace with a backslash character.
232.El
233.Pp
234The sequences for <single-quote> and backslash are the only sequences
235which are recognized inside of a single-quoted string.
236The other sequences have no special meaning inside a single-quoted
237string.
238All escape sequences are recognized inside of a double-quoted string.
239It is an error if a single
240.Ql \e
241character is followed by a character other than the ones listed above.
242.Pp
243The processing of
244.Fl S
245also supports substitution of values from environment variables.
246To do this, the name of the environment variable must be inside of
247.Ql ${} ,
248such as:
249.Li ${SOMEVAR} .
250The common shell syntax of
251.Li $SOMEVAR
252is not supported.
253All values substituted will be the values of the environment variables
254as they were when the
255.Nm
256utility was originally invoked.
257Those values will not be checked for any of the escape sequences as
258described above.
259And any settings of
260.Ar name Ns = Ns Ar value
261will not effect the values used for substitution in
262.Fl S
263processing.
264.Pp
265Also,
266.Fl S
267processing can not reference the value of the special parameters
268which are defined by most shells.
269For instance,
270.Fl S
271can not recognize special parameters such as:
272.Ql $* ,
273.Ql $@ ,
274.Ql $# ,
275.Ql $?
276or
277.Ql $$
278if they appear inside the given
279.Ar string .
280.\"
281.Ss Use in shell-scripts
282The
283.Nm
284utility is often used as the
285.Ar interpreter
286on the first line of interpreted scripts, as
287described in
288.Xr execve 2 .
289.Pp
290Note that the way the kernel parses the
291.Ql #!
292(first line) of an interpreted script has changed as of
293.Fx 6.0 .
294Prior to that, the
295.Fx
296kernel would split that first line into separate arguments based
297on any whitespace (space or <tab> characters) found in the line.
298So, if a script named
299.Pa /usr/local/bin/someport
300had a first line of:
301.Pp
302.Dl "#!/usr/local/bin/php -n -q -dsafe_mode=0"
303.Pp
304then the
305.Pa /usr/local/bin/php
306program would have been started with the arguments of:
307.Bd -literal -offset indent
308arg[0] = '/usr/local/bin/php'
309arg[1] = '-n'
310arg[2] = '-q'
311arg[3] = '-dsafe_mode=0'
312arg[4] = '/usr/local/bin/someport'
313.Ed
314.Pp
315plus any arguments the user specified when executing
316.Pa someport .
317However, this processing of multiple options on the
318.Ql #!
319line is not the way any other operating system parses the
320first line of an interpreted script.
321So after a change which was made for
322.Fx 6.0
323release, that script will result in
324.Pa /usr/local/bin/php
325being started with the arguments of:
326.Bd -literal -offset indent
327arg[0] = '/usr/local/bin/php'
328arg[1] = '-n -q -dsafe_mode=0'
329arg[2] = '/usr/local/bin/someport'
330.Ed
331.Pp
332plus any arguments the user specified.
333This caused a significant change in the behavior of a few scripts.
334In the case of above script, to have it behave the same way under
335.Fx 6.0
336as it did under earlier releases, the first line should be
337changed to:
338.Pp
339.Dl "#!/usr/bin/env -S /usr/local/bin/php -n -q -dsafe_mode=0"
340.Pp
341The
342.Nm
343utility will be started with the entire line as a single
344argument:
345.Pp
346.Dl "arg[1] = '-S /usr/local/bin/php -n -q -dsafe_mode=0'"
347.Pp
348and then
349.Fl S
350processing will split that line into separate arguments before
351executing
352.Pa /usr/local/bin/php .
353.\"
354.Sh ENVIRONMENT
355The
356.Nm
357utility uses the
358.Ev PATH
359environment variable to locate the requested
360.Ar utility
361if the name contains no
362.Ql /
363characters, unless the
364.Fl P
365option has been specified.
366.Sh EXIT STATUS
367.Ex -std
368An exit status of 126 indicates that
369.Ar utility
370was found, but could not be executed.
371An exit status of 127 indicates that
372.Ar utility
373could not be found.
374.Sh EXAMPLES
375Since the
376.Nm
377utility is often used as part of the first line of an interpreted script,
378the following examples show a number of ways that the
379.Nm
380utility can be useful in scripts.
381.Pp
382The kernel processing of an interpreted script does not allow a script
383to directly reference some other script as its own interpreter.
384As a way around this, the main difference between
385.Pp
386.Dl #!/usr/local/bin/foo
387and
388.Dl "#!/usr/bin/env /usr/local/bin/foo"
389.Pp
390is that the latter works even if
391.Pa /usr/local/bin/foo
392is itself an interpreted script.
393.Pp
394Probably the most common use of
395.Nm
396is to find the correct interpreter for a script, when the interpreter
397may be in different directories on different systems.
398The following example will find the
399.Ql perl
400interpreter by searching through the directories specified by
401.Ev PATH .
402.Pp
403.Dl "#!/usr/bin/env perl"
404.Pp
405One limitation of that example is that it assumes the user's value
406for
407.Ev PATH
408is set to a value which will find the interpreter you want
409to execute.
410The
411.Fl P
412option can be used to make sure a specific list of directories is
413used in the search for
414.Ar utility .
415Note that the
416.Fl S
417option is also required for this example to work correctly.
418.Pp
419.Dl "#!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl"
420.Pp
421The above finds
422.Ql perl
423only if it is in
424.Pa /usr/local/bin
425or
426.Pa /usr/bin .
427That could be combined with the present value of
428.Ev PATH ,
429to provide more flexibility.
430Note that spaces are not required between the
431.Fl S
432and
433.Fl P
434options:
435.Pp
436.Dl "#!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl"
437.Sh COMPATIBILITY
438The
439.Nm
440utility accepts the
441.Fl
442option as a synonym for
443.Fl i .
444.Sh SEE ALSO
445.Xr printenv 1 ,
446.Xr sh 1 ,
447.Xr execvp 3 ,
448.Xr environ 7
449.Sh STANDARDS
450The
451.Nm
452utility conforms to
453.St -p1003.1-2001 .
454The
455.Fl P , S , u
456and
457.Fl v
458options are non-standard extensions supported by
459.Fx ,
460but which may not be available on other operating systems.
461.Sh HISTORY
462The
463.Nm
464command appeared in
465.Bx 4.4 .
466The
467.Fl P , S
468and
469.Fl v
470options were added in
471.Fx 6.0 .
472.Sh BUGS
473The
474.Nm
475utility does not handle values of
476.Ar utility
477which have an equals sign
478.Pq Ql =
479in their name, for obvious reasons.
480.Pp
481The
482.Nm
483utility does not take multibyte characters into account when
484processing the
485.Fl S
486option, which may lead to incorrect results in some locales.
487