🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

env Command

Beginner Shell Scripting man(1)

Display or set environment variables

👁 11 views 📅 Updated: Mar 15, 2026
SYNTAX
env [OPTION]... [NAME=VALUE]... [COMMAND]

What Does env Do?

env displays environment variables or runs a command with a modified environment. Without arguments, it prints all current environment variables. With arguments, it sets variables for a single command execution.

env is useful for running commands with specific environment settings without modifying the current shell, finding the correct interpreter in scripts (#!/usr/bin/env python3), and debugging environment issues.

The #!/usr/bin/env python3 pattern in script shebangs is widely used because it finds the python3 executable in the user's PATH, making scripts more portable across different systems.

Options & Flags

OptionDescriptionExample
-i Start with empty environment env -i PATH=/usr/bin bash
-u Unset a variable env -u HTTP_PROXY curl https://example.com
VAR=val Set variable for the command env NODE_ENV=production node app.js
(no args) Print all environment variables env
-0 End each line with NUL instead of newline env -0 | sort -z
#!/usr/bin/env Find interpreter in PATH (shebang) #!/usr/bin/env python3

Practical Examples

#1 Show all variables

Displays all current environment variables.
$ env
Output: HOME=/home/user\nPATH=/usr/bin:/bin\nSHELL=/bin/bash

#2 Run with custom environment

Runs node with NODE_ENV set to production, without affecting the current shell.
$ env NODE_ENV=production node server.js

#3 Run with empty environment

Starts a command with minimal environment variables.
$ env -i HOME=$HOME PATH=/usr/bin bash -c "echo clean shell"

#4 Unset variable for command

Runs curl without the http_proxy variable (bypasses proxy).
$ env -u http_proxy curl https://api.example.com

#5 Sort environment variables

Displays all variables in alphabetical order.
$ env | sort

#6 Portable shebang

Used in script first line to find python3 in the user's PATH.
$ #!/usr/bin/env python3

Tips & Best Practices

#!/usr/bin/env for scripts: Use #!/usr/bin/env python3 instead of #!/usr/bin/python3 in scripts. env finds the right interpreter regardless of where it is installed.
env vs printenv: env without args and printenv both show environment variables. env can also run commands; printenv can show a single variable: printenv HOME.
env -i is very minimal: env -i strips ALL variables. Most programs need at least HOME, PATH, and TERM to function properly.

Frequently Asked Questions

How do I set a variable for a single command?
Use env VAR=value command or simply VAR=value command. The variable only exists for that command.
What does #!/usr/bin/env do?
In script shebangs, env searches PATH for the specified interpreter. This is more portable than hard-coding the path.
How do I clear all environment variables?
Use env -i command to run a command with an empty environment. Add specific variables: env -i PATH=/usr/bin HOME=$HOME command.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →