Env Command Guide
Understanding the Env Command
The env
command in Unix-like operating systems is a powerful utility for managing environment variables. It allows you to view, set, unset, and execute commands within a modified environment. This guide will walk you through its common uses and syntax.
Listing Environment Variables
To display all exported environment variables along with their current values, simply run the env
command without any arguments.
env
Setting Environment Variables for a Command
You can set a specific environment variable for the duration of a single command. This is useful for temporarily configuring an application or script without altering your global environment.
env <variable>=<value> <command>
For instance, to set a DEBUG
variable to true
for a script named my_script.sh
:
env DEBUG=true ./my_script.sh
Setting Multiple Environment Variables
The env
command also supports setting multiple environment variables simultaneously before executing a command.
env <variable_1>=<value> <variable_2>=<value> <command>
Example:
env API_KEY=abcdef123 USER=admin ./run_app.sh
Unsetting Environment Variables
To execute a command with a specific environment variable removed from its inherited environment, use the -u
option.
env -u <variable> <command>
This is helpful when you want to ensure a command does not pick up a particular variable from the parent shell.
Clearing the Environment
The -i
option allows you to run a command in an entirely clean environment, meaning it will not inherit any variables from the current shell. This is the most restrictive way to run a command.
env -i <command>
This is often used for testing or ensuring a command runs in a predictable, isolated state.
Further Reading
env
man page- MDN Web Docs on Dates (for context on time-related variables)