Linux env Command Complete Guide
env is a very practical 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 users; they affect the behavior of running processes. The env command provides us with a convenient way to view and manipulate these variables.
Analogy: Think of environment variables as a bulletin board in an office. All staff members (programs) can see the information (environment variables) on it and adjust their work style based on this information.
Basic syntax of the env command
The basic syntax format of the env command is as follows:
env [OPTION]... [NAME=VALUE]... [COMMAND [ARG]...]
If you run it directly without any parametersenv, it will display all current environment variables and their values.
Common options and parameters
The env command supports the following common options:
| Option | Description |
|---|---|
-i, --ignore-environment |
Start from an empty environment, ignoring inherited environment variables |
-u, --unset=NAME |
Remove specified variables from the environment |
-C, --chdir=DIR |
Change to the specified directory before running the command |
-S, --split-string=S |
Split the arguments into multiple arguments |
--help |
Display help information |
--version |
Display version information |
Basic usage examples
1. View all environment variables
The simplest usage is to directly enter theenvcommand:
Example
This will output all environment variables in the current shell in the formatVariable name=value, for example:
USER=john HOME=/home/john PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
2. Run commands in a custom environment
You can temporarily set environment variables to run a program:
Example
In this example, we temporarily setLANG=Cand then run thelscommand, which will affect thelsoutput language of the command.
3. Create a clean environment to run programs
Use the-ioption to create a clean environment (without inheriting any existing environment variables):
Example
Here we created a clean environment with onlyPATHvariable to run thelscommand.
Advanced usage
1. Temporarily modify variables to run scripts
Example
This command will temporarily set theEDITORenvironment variable tonano, then runcrontab -e, which will use the nano editor to edit crontab.
2. Remove specific environment variables
Example
This command will when runninglsremove theHOMEenvironment variable.
3. Use with shebang
env is often used in the shebang line of scripts to make them more portable:
Example
This approach is more flexible than directly specifying/bin/bashmore flexible, because it willPATHlook for the location of bash in the environment variable.
Practical application scenarios
1. Debug environment variable issues
When a program behaves abnormally, you can use env to check its runtime environment:
Example
This can rule out environment variable interference and determine whether the issue is caused by environment variables.
2. Safely run untrusted scripts
When running scripts of unknown origin, you can create a restricted environment:
Example
3. Test program behavior under different locale environments
Example
env LANG=zh_CN.UTF-8 program
This allows testing the program's behavior under different locale/language environments.
Notes
- Variable overwriting: Variables set via env will overwrite existing environment variables with the same name.
- Child process impact: The environment variables modified by env only affect the child processes it starts, not the current shell.
- Security: Sensitive information (such as passwords) should not be passed through environment variables.
- Variable order: Variables set later will overwrite variables with the same name set earlier.