Linux Command Reference ยท Processes and Signals
Linux env command
Reference for the Linux env command: print the environment, run a program with extra or removed variables, start from an empty environment and use env in shebangs.
env does two things. Without arguments it prints the current environment, one NAME=value pair per line. With a command it runs that command in a modified copy of the environment: you can add variables, remove some, or throw everything away first. The change applies only to that one child process; your shell is not touched.
It is also the portable way to start an interpreter from a script: #!/usr/bin/env python3 finds the interpreter on PATH instead of hard-coding its location.
env [OPTION]... [NAME=VALUE]... [COMMAND [ARG]...]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
env [-i] [-0] [-u NAME]... [-C DIR] [-S STRING] [NAME=VALUE]... [COMMAND [ARG]...]
| Name | Description |
|---|---|
NAME=VALUE |
Set NAME to VALUE in the child's environment. |
-i, --ignore-environment, - |
Start with an empty environment instead of a copy of the current one. |
-u NAME, --unset=NAME |
Remove NAME from the child's environment. Repeatable. |
-0, --null |
Without a command, end each printed line with a NUL byte instead of a newline. |
-C DIR, --chdir=DIR |
Change to DIR before running the command (GNU coreutils 8.28 and newer). |
-S STRING, --split-string=STRING |
Split STRING into separate arguments, so that a single shebang argument can carry several options (8.30 and newer). |
COMMAND |
Program to run. Found through PATH; shell builtins and functions are not available. |
Returns: with a command, the command's exit status. If the command cannot be found, 127; if it exists but cannot be run, 126; if env itself fails (bad option), 125. Without a command, 0 after printing.
Usage notes
- Only exported variables.
envshows and passes on the environment, not every shell variable. A plainx=1that was never exported does not appear. Compare withexportandset. envversus a prefix assignment.NAME=value commanddoes the same job for a single command, built into the shell.envadds-i,-uand the ability to start a program from another program such asxargsorssh, where no shell syntax is available.env -iis harsh. With an empty environment there is noPATH; the shell used to look upcommandmay still find it, but the program itself may fail to find helpers or its locale. Pass what is needed:env -i PATH=/usr/bin:/bin HOME="$HOME" cmd.- Shebang use.
#!/usr/bin/env bashworks on systems where Bash is not in/bin. Many kernels pass everything after the interpreter as one argument, so#!/usr/bin/env python3 -ufails;#!/usr/bin/env -S python3 -usplits it correctly. - Reproducible runs.
env -iis a quick way to test whether a script relies on variables from your login environment, which is a common reason for "works in my terminal, fails in cron". - Sorting the output. Variable order is not guaranteed; pipe to
sortfor stable results, as above. - Multi-line values make the plain output ambiguous. Use
-0and a NUL-aware reader such asxargs -0orread -d ''. printenv NAMEprints just the value of one variable, without theNAME=.
Related
export,exec,xargs,nohup- Lessons: Environment, Variables, First Script