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