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