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