{{Short description | Shell command for reporting and running with modified environment variables}} {{lowercase title}} {{About|the Unix shell command|the viral gene|Env (gene)|other uses|Env (disambiguation)}} {{Infobox software | name = env | logo = | screenshot = Env Void Linux.png | screenshot size = | caption = <code>env</code> command on a Void Linux machine. | developer = | released = | latest release version = | latest release date = | operating system = Unix, Unix-like, Inferno | platform = Cross-platform | genre = Command | license = coreutils: GPLv3+ | website = }} <code>'''env'''</code> 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.
By default, a subprocess inherits the environment variables of the parent process. <code>env</code> supports adding, modifying and removing the copied variables. <code>env</code> can also be used to launch the correct interpreter.{{clarify | correct in what sense?|date=December 2025}} 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 {{code |export}} command and assigning a name to a value like {{code | 1=NAME=value}}.
The GNU coreutils version was written by Richard Mlynarik, David MacKenzie, and Assaf Gordon.<ref name=gnu>{{man|1|env|Linux}}</ref> It first appeared in 4.4BSD, and is a part of POSIX.1 (with the {{tt|-i}} option only).<ref name=fbsd>{{man|1|env|FreeBSD}}</ref> This version has been extended to handle signals and the current directory.<ref name=gnu/>
FreeBSD's version supports a custom search path. Extensions found in both versions include {{tt|-u}}, for unsetting variables, and {{tt|-S}}, for splitting arguments (mainly in shebang).<ref name=fbsd/>
==Examples== The following command line prints the current environment variables to standard output: <syntaxhighlight lang="bash"> $ env </syntaxhighlight>
The following command line creates a new shell without any environment variables: <syntaxhighlight lang="bash"> $ env -i /bin/sh </syntaxhighlight>
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: <syntaxhighlight lang="bash"> $ env DISPLAY=foo.bar:1.0 xcalc </syntaxhighlight>
This use of env is often unnecessary since most shells support setting environment variables in front of a command like: <syntaxhighlight lang="bash"> $ DISPLAY=foo.bar:1.0 xcalc </syntaxhighlight>
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: <syntaxhighlight lang="bash"> #!/usr/bin/env python3 print("Hello, World!") </syntaxhighlight>
In this example, <code>/usr/bin/env</code> is the full path of the <code>env</code> command. The environment is not altered.
Note that it is possible to specify the interpreter without using <code>env</code>, by giving the full path of the <code>python</code> interpreter. A problem with that approach is that on different computer systems, the exact path may be different. By instead using <code>env</code> as in the example, the interpreter is searched for and located at the time the script is run (more precisely, <code>env</code> does a system call to <code>execvp</code>, 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 <code>env</code> binary may also be different on a per-machine basis.
==See also== {{Portal|Linux}} * List of POSIX commands * set
==References== {{Reflist}}
==External links== {{Wikibooks|Guide to Unix|Commands}} * {{man|cu|env|SUS|set the environment for command invocation}} * {{man|1|env|Inferno}} * [https://www.gnu.org/software/coreutils/manual/html_node/env-invocation.html env]—manual page from GNU coreutils. * {{man|1|env|FreeBSD}} * {{man|1|env|OpenBSD|run a program in a modified environment}} * {{man|1|env|NetBSD|set and print environment}} * {{man|1|env|Linux}} * {{man|1|env|Solaris}}
{{Unix commands}} {{Core Utilities commands}}
Category:Standard Unix programs Category:Unix SUS2008 utilities Category:Inferno (operating system) commands Category:IBM i Qshell commands Category:Environment variables