ENV(1) General Commands Manual ENV(1)

envset environment for command invocation

env [−i] [name=value] . . . [utility [argument . . . ]]
Obsolescent Version:
env [−] [name=value] . . . [utility [argument . . . ]]

The env utility shall obtain the current environment, modify it according to its arguments, then invoke the utility named by the utility operand with the modified environment. Optional arguments shall be passed to utility. If no utility operand is specified, the resulting environment shall be written to the standard output, with one name=value pair per line.

The env utility shall conform to the utility argument syntax guidelines described in 2.10.2, except for its nonstandard usage of −, which is obsolescent. The following options shall be supported by the implementation:

Invoke utility with exactly the environment specified by the arguments; the inherited environment shall be ignored completely. − (Obsolescent.) Equivalent to the −i option.

The following operands shall be supported by the implementation: name=value Arguments of the form name=value modify the execution environment, and are placed into the inherited environment before the utility is invoked.

utility

The name of the utility to be invoked. If the utility operand names any of the special built-in utilities in 3.14, the results are undefined.

argument

A string to pass as an argument for the invoked utility.

None.

None.

The following environment variables shall affect the execution of env:

This variable shall determine the locale to use for the locale categories when both LC_ALL and the corresponding environment variable (beginning with LC_ ) do not specify a locale. See 2.6.

This variable shall determine the locale to be used to override any values for locale categories specified by the settings of LANG or any environment variables beginning with LC_.

This variable shall determine the locale for the interpretation of sequences of bytes of text data as characters (e.g., single- versus multibyte characters in arguments).

This variable shall determine the language in which messages should be written.

This variable shall determine the location of the utility, as described in 2.6. If PATH is specified as a name=value operand to env, the value given shall be used in the search for utility.

Default.

If no utility operand is specified, each name=value pair in the resulting environment shall be written in the form: "%s=%s\n", <name>, <value>

If the utility operand is specified, the env utility shall not write to standard output.

Used only for diagnostic messages.

None.

None.

If the utility utility is invoked, the exit status of env shall be the exit status of utility; otherwise, the env utility shall exit with one of the following values:

The env utility completed successfully. 1−125 An error occurred in the env utility.

The utility specified by utility was found but could not be invoked.

The utility specified by utility could not be found.

Default.

The following command: env -i PATH =/mybin mygrep xyz myfile invokes the command mygrep with a new PATH value as the only entry in its environment. In this case, PATH is used to locate mygrep, which then must reside in /mybin. As with all other utilities that invoke other utilities, the standard only specifies what env does with standard input, standard output, standard error, input files, and output files. If a utility is executed, it is not constrained by env ’s specification of input and output.

The command, env, nohup, and xargs utilities have been specified to use exit code 127 if an error occurs so that applications can distinguish ‘‘failure to find a utility’’ from ‘‘invoked utility exited with an error indication.’’ The value 127 was chosen because it is not commonly used for other meanings; most utilities use small values for ‘‘normal error conditions’’ and the values above 128 can be confused with termination due to receipt of a signal. The value 126 was chosen in a similar manner to indicate that the utility could be found, but not invoked. Some scripts produce meaningful error messages differentiating the 126 and 127 cases. The distinction between exit codes 126 and 127 is based on KornShell practice that uses 127 when all attempts to exec the utility fail with ENOENT, and uses 126 when any attempt to exec the utility fails for any other reason.

The −i option was added to allow the functionality of the − option in a manner compatible with the Utility Syntax Guidelines. It is the intent of the working group that portable applications use the −i option, and that in the future the − option be removed from the standard. Historical implementations of the env utility use () or () (see . 1 {8} 3.1.2) to invoke the specified utility; this provides better performance and keeps users from having to escape characters with special meaning to the shell. Therefore, shell functions, special built-ins, and built-ins that are only provided by the shell are not found. Implementations are free to invoke a shell instead of using one of the exec family of routines, but if they do, they must be sure to escape any characters with special meaning to the shell so that the user does not have to be aware of the difference. Some have suggested that env is redundant since the same effect is achieved by: name=value . . . utility [argument . . . ] The example is equivalent to env when an environment variable is being added to the environment of the command, but not when the environment is being set to the given value. The env utility also writes out the current environment if invoked without arguments. There is sufficient functionality beyond what the example provides to justify inclusion of env.

September 1991 posix.fail