1ENV(1P) POSIX Programmer's Manual ENV(1P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 env — set the environment for command invocation
13
15 env [-i] [name=value]... [utility [argument...]]
16
18 The env utility shall obtain the current environment, modify it accord‐
19 ing to its arguments, then invoke the utility named by the utility op‐
20 erand with the modified environment.
21
22 Optional arguments shall be passed to utility.
23
24 If no utility operand is specified, the resulting environment shall be
25 written to the standard output, with one name=value pair per line.
26
27 If the first argument is '-', the results are unspecified.
28
30 The env utility shall conform to the Base Definitions volume of
31 POSIX.1‐2017, Section 12.2, Utility Syntax Guidelines, except for the
32 unspecified usage of '-'.
33
34 The following options shall be supported:
35
36 -i Invoke utility with exactly the environment specified by the
37 arguments; the inherited environment shall be ignored com‐
38 pletely.
39
41 The following operands shall be supported:
42
43 name=value
44 Arguments of the form name=value shall modify the execution
45 environment, and shall be placed into the inherited environ‐
46 ment before the utility is invoked.
47
48 utility The name of the utility to be invoked. If the utility operand
49 names any of the special built-in utilities in Section 2.14,
50 Special Built-In Utilities, the results are undefined.
51
52 argument A string to pass as an argument for the invoked utility.
53
55 Not used.
56
58 None.
59
61 The following environment variables shall affect the execution of env:
62
63 LANG Provide a default value for the internationalization vari‐
64 ables that are unset or null. (See the Base Definitions vol‐
65 ume of POSIX.1‐2017, Section 8.2, Internationalization Vari‐
66 ables for the precedence of internationalization variables
67 used to determine the values of locale categories.)
68
69 LC_ALL If set to a non-empty string value, override the values of
70 all the other internationalization variables.
71
72 LC_CTYPE Determine the locale for the interpretation of sequences of
73 bytes of text data as characters (for example, single-byte as
74 opposed to multi-byte characters in arguments).
75
76 LC_MESSAGES
77 Determine the locale that should be used to affect the format
78 and contents of diagnostic messages written to standard
79 error.
80
81 NLSPATH Determine the location of message catalogs for the processing
82 of LC_MESSAGES.
83
84 PATH Determine the location of the utility, as described in the
85 Base Definitions volume of POSIX.1‐2017, Chapter 8, Environ‐
86 ment Variables. If PATH is specified as a name=value operand
87 to env, the value given shall be used in the search for util‐
88 ity.
89
91 Default.
92
94 If no utility operand is specified, each name=value pair in the result‐
95 ing environment shall be written in the form:
96
97
98 "%s=%s\n", <name>, <value>
99
100 If the utility operand is specified, the env utility shall not write to
101 standard output.
102
104 The standard error shall be used only for diagnostic messages.
105
107 None.
108
110 None.
111
113 If utility is invoked, the exit status of env shall be the exit status
114 of utility; otherwise, the env utility shall exit with one of the fol‐
115 lowing values:
116
117 0 The env utility completed successfully.
118
119 1-125 An error occurred in the env utility.
120
121 126 The utility specified by utility was found but could not be
122 invoked.
123
124 127 The utility specified by utility could not be found.
125
127 Default.
128
129 The following sections are informative.
130
132 The command, env, nice, nohup, time, and xargs utilities have been
133 specified to use exit code 127 if an error occurs so that applications
134 can distinguish ``failure to find a utility'' from ``invoked utility
135 exited with an error indication''. The value 127 was chosen because it
136 is not commonly used for other meanings; most utilities use small val‐
137 ues for ``normal error conditions'' and the values above 128 can be
138 confused with termination due to receipt of a signal. The value 126 was
139 chosen in a similar manner to indicate that the utility could be found,
140 but not invoked. Some scripts produce meaningful error messages differ‐
141 entiating the 126 and 127 cases. The distinction between exit codes 126
142 and 127 is based on KornShell practice that uses 127 when all attempts
143 to exec the utility fail with [ENOENT], and uses 126 when any attempt
144 to exec the utility fails for any other reason.
145
146 Historical implementations of the env utility use the execvp() or exe‐
147 clp() functions defined in the System Interfaces volume of POSIX.1‐2017
148 to invoke the specified utility; this provides better performance and
149 keeps users from having to escape characters with special meaning to
150 the shell. Therefore, shell functions, special built-ins, and built-ins
151 that are only provided by the shell are not found.
152
154 The following command:
155
156
157 env -i PATH=/mybin:"$PATH" $(getconf V7_ENV) mygrep xyz myfile
158
159 invokes the command mygrep with a new PATH value as the only entry in
160 its environment other than any variables required by the implementation
161 for conformance. In this case, PATH is used to locate mygrep, which is
162 expected to reside in /mybin.
163
165 As with all other utilities that invoke other utilities, this volume of
166 POSIX.1‐2017 only specifies what env does with standard input, standard
167 output, standard error, input files, and output files. If a utility is
168 executed, it is not constrained by the specification of input and out‐
169 put by env.
170
171 The -i option was added to allow the functionality of the removed -
172 option in a manner compatible with the Utility Syntax Guidelines. It is
173 possible to create a non-conforming environment using the -i option, as
174 it may remove environment variables required by the implementation for
175 conformance. The following will preserve these environment variables as
176 well as preserve the PATH for conforming utilities:
177
178
179 IFS='
180 '
181 # The preceding value should be <space><tab><newline>.
182 # Set IFS to its default value.
183
184 set -f
185 # disable pathname expansion
186
187 \unalias -a
188 # Unset all possible aliases.
189 # Note that unalias is escaped to prevent an alias
190 # being used for unalias.
191 # This step is not strictly necessary, since aliases are not inherited,
192 # and the ENV environment variable is only used by interactive shells,
193 # the only way any aliases can exist in a script is if it defines them
194 # itself.
195
196 unset -f env getconf
197 # Ensure env and getconf are not user functions.
198
199 env -i $(getconf V7_ENV) PATH="$(getconf PATH)" command
200
201 Some have suggested that env is redundant since the same effect is
202 achieved by:
203
204
205 name=value ... utility [ argument ... ]
206
207 The example is equivalent to env when an environment variable is being
208 added to the environment of the command, but not when the environment
209 is being set to the given value. The env utility also writes out the
210 current environment if invoked without arguments. There is sufficient
211 functionality beyond what the example provides to justify inclusion of
212 env.
213
215 None.
216
218 Section 2.14, Special Built-In Utilities, Section 2.5, Parameters and
219 Variables
220
221 The Base Definitions volume of POSIX.1‐2017, Chapter 8, Environment
222 Variables, Section 12.2, Utility Syntax Guidelines
223
225 Portions of this text are reprinted and reproduced in electronic form
226 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
227 table Operating System Interface (POSIX), The Open Group Base Specifi‐
228 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
229 Electrical and Electronics Engineers, Inc and The Open Group. In the
230 event of any discrepancy between this version and the original IEEE and
231 The Open Group Standard, the original IEEE and The Open Group Standard
232 is the referee document. The original Standard can be obtained online
233 at http://www.opengroup.org/unix/online.html .
234
235 Any typographical or formatting errors that appear in this page are
236 most likely to have been introduced during the conversion of the source
237 files to man page format. To report such errors, see https://www.ker‐
238 nel.org/doc/man-pages/reporting_bugs.html .
239
240
241
242IEEE/The Open Group 2017 ENV(1P)