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