What it does
Without arguments, env prints all environment variables, one NAME=value per line. These are the variables that programs started from your shell receive.
With a command after it, env runs that command with extra, removed or no variables at all. The main shell does not change.
You also see env in the first line of scripts: #!/usr/bin/env python3 finds python3 through $PATH, so the script works on systems where the program is in another folder.
How to write it
env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]Options 13
Most used
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1Show all environment variables
envSorted output is easier to read:
env | sort.2Find one variable
env | grep -i proxyShows HTTP_PROXY, https_proxy and others. Proxy variables often explain why
curlordnffails.3Run one command with an extra variable
env LANG=C df -hThe command runs in English with standard formats. Good for scripts that read command output.
4Run a command without a variable
env -u HTTP_PROXY -u HTTPS_PROXY curl -I https://intranet.example.comSkip the proxy for one internal test without changing your shell.
5Run a program in a clean environment
env -i PATH=/usr/bin:/bin HOME=/tmp bash -c 'env'Shows what a program sees with almost no variables. Close to what cron and systemd give a job.
6Use env in the first line of a script
head -1 deploy.py #!/usr/bin/env python3The script uses the first
python3in $PATH, for example from a virtual environment.7Pass options to the interpreter in a shebang
head -1 tool.sh #!/usr/bin/env -S bash -euo pipefail-Ssplits the text into several arguments. Without it, the line does not work on Linux.8Compare the environment of root and your user
sudo resets most variables. This shows what changes.
9Check the variables of a running process
Each process keeps the environment it started with. Good to debug a service.
10Run a command in another folder
env -C /var/www/html ls -lThe shell stays in its own folder.
11Print variables safely for a script (NUL separated)
NUL separators are safe even when a value contains a new line.
Nothing matches this filter.
Watch out
envshows only exported variables. A plain shell variable (X=1without export) is not in the list; usesetfor all.- Secrets in environment variables can be read by root and by tools that dump the environment. Prefer files with tight permissions.
env NAME=valuechanges the variable only for that one command.
Files
/proc/PID/environ- The environment of a running process (NUL separated).
/etc/environment- Variables set for all users at login (simple NAME=value lines).
Tips to remember
env= show,env NAME=val cmd= one command with a change,env -i= clean start.