| env | |
|---|---|
env command on aVoid Linux machine. | |
| Operating system | Unix,Unix-like,Inferno |
| Platform | Cross-platform |
| Type | Command |
| License | coreutils:GPLv3+ |
env is ashellcommand that either reportsenvironment variables or runs a command in asubprocess with modified environment variables. The command is provided in aUnix-like system.
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 correctinterpreter.[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 theexport command and assigning a name to a value likeNAME=value.
TheGNUcoreutils version was written by Richard Mlynarik, David MacKenzie, and Assaf Gordon.[1] It first appeared in4.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]
The following command line prints the current environment variables tostandard output:
$env
The following command line creates a new shell without any environment variables:
$env-i/bin/sh
The following command line executes the applicationxcalc 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 thehashbang line of a script to allow theinterpreter to be looked up via the PATH. For example, here is the code of aPython script:
#!/usr/bin/env python3print("Hello, World!")
In this example,/usr/bin/env is the fullpath of theenv command. The environment is not altered.
Note that it is possible to specify the interpreter without usingenv, by giving the full path of thepython interpreter. A problem with that approach is that on different computer systems, the exact path may be different. By instead usingenv 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 toexecvp, which does the job of locating the interpreter and launching it). This makes the script moreportable, 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 theenv binary may also be different on a per-machine basis.
env: set the environment for command invocation – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Groupenv(1) – Inferno General commandsManualenv(1) – FreeBSD General CommandsManualenv(1): run a program in a modified environment – OpenBSD General CommandsManualenv(1): set and print environment – NetBSD General CommandsManualenv(1) – Linux UserManual – User Commands from Manned.orgenv(1) – Solaris 11.4 User Commands ReferenceManual