Env

Last updated
env
Operating system Unix, Unix-like, Inferno
Platform Cross-platform
Type Command
License coreutils: GPLv3+

env is a shell command that either reports environment variables or runs a command in a subprocess with modified environment variables. The command is provided in a Unix-like system.

Contents

By default, a subprocess inherits the environment variables of the parent process. env supports adding, modifying and removing the copied variables. env can also be used to launch the correct interpreter.[ clarification needed ] In this usage, the environment is typically not changed. The command does not modify environment variables in the process in which it runs. Mechanisms for doing so include the export command and assigning a name to a value like NAME=value.

The GNU coreutils version was written by Richard Mlynarik, David MacKenzie, and Assaf Gordon. [1] It first appeared in 4.4BSD, and is a part of POSIX.1 (with the -i option only). [2] This version has been extended to handle signals and the current directory. [1]

FreeBSD's version supports a custom search path. Extensions found in both versions include -u, for unsetting variables, and -S, for splitting arguments (mainly in shebang). [2]

Examples

The following command line prints the current environment variables to standard output:

$env 

The following command line creates a new shell without any environment variables:

$env-i/bin/sh 

The following command line executes the application xcalc such that the variable DISPLAY has value "foo.bar:1.0" so that it shows on the specified display:

$envDISPLAY=foo.bar:1.0xcalc 

This use of env is often unnecessary since most shells support setting environment variables in front of a command like:

$DISPLAY=foo.bar:1.0xcalc 

env may also be used in the hashbang line of a script to allow the interpreter to be looked up via the PATH. For example, here is the code of a Python script:

#!/usr/bin/env python3 print("Hello, World!")

In this example, /usr/bin/env is the full path of the env command. The environment is not altered.

Note that it is possible to specify the interpreter without using env, by giving the full path of the python interpreter. A problem with that approach is that on different computer systems, the exact path may be different. By instead using env as in the example, the interpreter is searched for and located at the time the script is run (more precisely, env does a system call to execvp, which does the job of locating the interpreter and launching it). This makes the script more portable, but also increases the risk that the wrong interpreter is selected because it searches for a match in every directory on the executable search path. It also suffers from the same problem in that the path to the env binary may also be different on a per-machine basis.

See also

References

  1. 1 2 env(1)    Linux User Manual – User Commands from Manned.org
  2. 1 2 env(1)    FreeBSD General Commands Manual