A Complete Guide to the Linux env Command
env is a very useful command-line tool in Linux/Unix systems, mainly used to display and modify environment variables and to run programs in a custom environment.
Environment variables are dynamic values set by the operating system or the user, and they affect the behavior of running processes. The env command provides a convenient way to view and manipulate these variables.
An analogy: you can think of environment variables as a bulletin board in an office, where all the staff (programs) can see the information (environment variables) on it and adjust the way they work accordingly.
Basic syntax of the env command
The basic syntax of the env command is as follows:
env [OPTION]... [NAME=VALUE]... [COMMAND [ARG]...]
If you run it directly without any arguments env, it displays all current environment variables and their values.
Commonly used options
The env command supports the following common options:
| Option | Description |
|---|---|
-i, --ignore-environment |
Starts with an empty environment, ignoring inherited environment variables |
-u, --unset=NAME |
Removes the specified variable from the environment |
-C, --chdir=DIR |
Changes to the specified directory before running the command |
-S, --split-string=S |
Splits an argument into multiple arguments |
--help |
Display help information |
--version |
Display version information |
Basic Usage Example
1. View all environment variables
The simplest usage is to simply type env Command:
Example
env
This outputs all environment variables in the current shell, in the format 变量名=值, for example:
USER=john
HOME=/home/john
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
2. Run a command in a custom environment
You can temporarily set environment variables to run a program:
Example
env
LANG
=C
ls
In this example, we temporarily set LANG=C and then run ls command, which affects the ls command's output language.
3. Run a program in a clean environment
Use -i option creates a clean environment (inheriting no existing environment variables):
Example
env
-i
PATH
=
/
bin:
/
usr
/
bin
ls
Here we create a clean environment with only the PATH variable to run ls command.
Advanced usage
1. Temporarily modify a variable to run a script
Example
env
EDITOR
=
nano
crontab
-e
This command temporarily sets the EDITOR environment variable to nano, and then runs crontab -e, so that the nano editor is used to edit crontab.
2. Remove a specific environment variable
Example
env
-u
HOME
ls
This command, when running ls removes the HOME environment variable.
3. Use with a shebang
env is often used in the shebang line of a script to make it more portable:
Example
#!/usr/bin/env bash
This form is more flexible than directly specifying /bin/bash , because it searches the PATH environment variable for the location of bash.
Practical Application Scenarios
1. Debug environment variable issues
When a program behaves abnormally, you can use env to check the environment in which it runs:
Example
env
-i
/
path
/
to
/
program
This rules out interference from environment variables and helps determine whether the problem is caused by them.
2. Safely run untrusted scripts
When running a script from an unknown source, you can create a restricted environment:
Example
env
-i
PATH
=
/
bin:
/
usr
/
bin
/
path
/
to
/
script.sh
3. Test program behavior under different language environments
Example
env
LANG
=fr_FR.UTF-
8
program
env
LANG
=zh_CN.UTF-
8
program
This lets you test how a program behaves under different language environments.
Notes
- Variable overriding: variables set through env override existing environment variables with the same name
- Child process impact: environment variables modified by env only affect the child processes it starts, not the current shell
- Safety: sensitive information (such as passwords) should not be passed through environment variables
- Variable order: variables set later override earlier variables with the same name
Linux Command Reference