Linux env Command | Rookie Tutorial
Rookie Tutorial --
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
Linux Tutorial
Linux TutorialLinux IntroductionLinux InstallationLinux Cloud ServerWSL Install LinuxLinux System Boot ProcessLinux System Directory StructureLinux Forget Password SolutionLinux Remote LoginLinux File Basic AttributesLinux File and Directory ManagementLinux User and Group ManagementLinux Disk ManagementLinux vi/vimlinux yum commandLinux apt command
Shell Tutorial
Shell TutorialShell VariablesShell Passing ArgumentsShell ArraysShell OperatorsShell echo CommandShell printf CommandShell test CommandShell Process ControlShell FunctionsShell Input/Output RedirectionShell File Inclusion
Linux Reference Manual
Linux Command ManualNginx Installation ConfigurationMySQL Installation ConfigurationLinux Quiz
Nginx Installation Configuration
Complete Guide to Linux env Command
env is a very practical command-line tool in Linux/Unix systems, mainly used for displaying and modifying environment variables, as well as running programs in a customized environment.
Environment variables are dynamic values set by the operating system or users that affect the behavior of running processes. The env command provides us with a convenient way to view and manipulate these variables.
Analogy: You can think of environment variables as a bulletin board in an office, where all staff (programs) can see the information (environment variables) and adjust their work accordingly.
Basic Syntax of env Command
The basic syntax format of the env command is as follows:
env ... ... [COMMAND ...]
If you run env without any parameters, it will display all current environment variables and their values.
Common Option Parameters
The env command supports the following common options:
env without any parameters, it will display all current environment variables and their values.| Option | Description |
|---|---|
-i, --ignore-environment |
Start with an empty environment, ignoring inherited environment variables |
-u, --unset=NAME |
Remove the specified variable from the environment |
-C, --chdir=DIR |
Change to the specified directory before running the command |
-S, --split-string=S |
Split 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 input the env command:
Example
env
This will output all environment variables in the current shell, in the format variable_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 programs:
Example
env LANG=C ls
In this example, we temporarily set LANG=C and then run the ls command, which will affect the output language of the ls command.
3. Create a Clean Environment to Run Programs
Use the -i option to create a clean environment (without inheriting any existing environment variables):
Example
env -i PATH=/bin:/usr/bin ls
Here we create a clean environment with only the PATH variable to run the ls command.
Advanced Usage
1. Temporarily Modify Variables to Run Scripts
Example
env EDITOR=nano crontab -e
This command will temporarily set the EDITOR environment variable to nano, then run crontab -e, which will use the nano editor to edit the crontab.
2. Remove Specific Environment Variables
Example
env -u HOME ls
This command will remove the HOME environment variable when running ls.
3. Combined Use with Shebang
env is often used in the shebang line of scripts to make scripts more portable:
Example
#!/usr/bin/env bash
This approach is more flexible than directly specifying /bin/bash, as it will search for the bash location in the PATH environment variable.
Practical Application Scenarios
1. Debug Environment Variable Issues
When program behavior is abnormal, you can use env to check the environment in which it runs:
Example
env -i /path/to/program
This can eliminate environment variable interference and determine whether the problem is caused by environment variables.
2. Securely Run Untrusted Scripts
When running scripts from unknown sources, you can create a restricted environment:
Example
env -i PATH=/bin:/usr/bin /path/to/script.sh
3. Test Program Behavior in Different Language Environments
Example
env LANG=fr_FR.UTF-8 program
env LANG=zh_CN.UTF-8 program
This can test the program's performance in different language environments.
Notes
- Variable Overwriting: Variables set through env will override existing environment variables with the same name
- Subprocess Impact: 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 override earlier variables with the same name
YouTip