|Operating system||Unix, Unix-like, Inferno|
env is a shell command for Unix and Unix-like operating systems. It is used to either print a list of environment variables or run another utility in an altered environment without having to modify the currently existing environment. Using
env, variables may be added or removed, and existing variables may be changed by assigning new values to them.
GNU's env has been extended to handle signals and the current directory. FreeBSD's env supports a custom search path. Extensions found in both versions include -u, for unsetting variables, and -S, for splitting arguments (mainly in shebang).
To print out the set of current environment variables:
To create a new environment without any existing environment variables for a new shell:
env -i /bin/sh
env DISPLAY=foo.bar:1.0 xcalc
Note that this use of env is often unnecessary since most shells support setting environment variables in front of a command:
#!/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.
- The Single UNIX Specification, Version 4 from The Open Group : set the environment for command invocation – Shell and Utilities Reference,
- Inferno General commands Manual –
- env—manual page from GNU coreutils.
- FreeBSD General Commands Manual –
- OpenBSD General Commands Manual : run a program in a modified environment –
- NetBSD General Commands Manual : set and print environment –
- Linux User Manual – User Commands –
- Solaris 10 User Commands Reference Manual –