Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

env - set environment for command execution

&pagelevel(4)&pagelevel

The env command can be used to display current environment variables and their values or to set them for the execution of a specific command. env inspects the current environment, modifies it according to the assignment in name=value, and then executes the command in the modified environment. The existing specifications for name and value are overwritten by the new ones, and the new arguments are merged into the inherited environment before the command is executed. The valid environment for the execution of command thus consists of the new specifications together with any unmodified environment variables.

If no command is specified, env prints the resulting environment.


Syntax


env[ -i| -][ name=value]...[ command[ arg...]]


–i | -

The original environment is ignored; command is then executed with exactly the environment specified by the arguments.

The i option corresponds to the old - option, which will continue to be supported.

name=value

name specifies the name of a variable that is to be valid for command.

value is the value of name which is to apply to command.

command

Name of the command or shell script which you would like to have executed in the defined environment.

arg

Argument, e.g. positional parameters or user-defined variables which can be passed to command.

Exit status

0The env utility was completed successfully
1-125Error
126The utility specified by command was found but could not be invoked.
127The utility specified by command could not be found.

Variable

PATH

Determine the location of the utility. If PATH is specified as a name=value operand to env, the value given will be used in the search for utility.

Locale

The following environment variables affect the execution of env:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1

Output the current values of environment variables:


$ env
_=/usr/bin/env

TTY=/dev/term/004

IO_CONVERSION=NO

PATH=/usr/bin:/opt/bin::/etc:/usr/sbin

TERM=BLOCK

HZ=100

LOGNAME=TESTUSER

PROGRAM_ENVIRONMENT=SHELL

HOME=/TESTUSER

TZ=MEZ-1MSZ-2,M3.5.0/02:00:00,M9.5.0/03:00:00

MAIL=/var/mail/TESTUSER

SHELL=/sbin/sh

USER=TESTUSER

LANG=

Example 2

Output the modified values of environment variables:


$ env PATH=$HOME/proz

_=/usr/bin/env

TTY=/dev/term/004

IO_CONVERSION=NO

PATH=/TESTUSER/proz

TERM=BLOCK

HZ=100

LOGNAME=TESTUSER

PROGRAM_ENVIRONMENT=SHELL

HOME=/TESTUSER

TZ=MEZ-1MSZ-2,M3.5.0/02:00:00,M9.5.0/03:00:00

MAIL=/var/mail/TESTUSER

SHELL=/sbin/sh

USER=TESTUSER

LANG=


The PATH environment variable has been modified.

Example 3

Output modified values of environment variables using the - option:


$ env - PATH=$HOME/proz

PATH=/TESTUSER/proz



The original environment is ignored.

Example 4

Invocation of the file ardor, located in /TESTUSER/SAYINGS, i.e. in a subdirectory of the HOME directory.

Contents of file ardor:

echo "$1 $2 $3 $4 $2 $3 $1 love $4!"

 

Now call ardor from any location in your file tree with the arguments I know that you:


$ env PATH=$HOME/SAYINGS ardor I know that you

I know that you know that I love you!



With the new definition of the PATH variable, you effectively define the location where the entered command is to be sought (the file ardor in our case), i.e. in a subdirectory of the HOME directory, which you specify with the value of the variable HOME ($HOME).

The character strings I, know, that, and you are passed as arguments to the positional parameters $1, $2, $3 and $4.

The contents of ardor are correctly executed here because echo is a built-in command of the sh shell. Due to the change in the PATH variable, all POSIX commands in /usr/bin, /usr/sbin or /opt/bin can no longer be found. To enable the execution of POSIX commands again, the PATH variable must be modified as shown in the example below.

Example 5

Call the file delcopy, which is located in the directory /TESTUSER/proc. This file contains a script that compares two files and deletes one of them if both files are identical.

Contents of the file delcopy:

if cmp -s $1 $2
then
rm $2
fi


Call the delcopy file from any location in your file tree with the arguments file1 and file2


$ env PATH=$PATH:$HOME/proc delcopy file1 file2


In this case, the new path is appended to the original one. This ensures that the delcopy script can be executed along with all commands contained in it. If only the path for delcopy were specified, the following error message would be issued.


/TESTUSER/proc/delcopy: cmp: not found

See also

sh, set, exec
profile, environ