1. What are Environment Variables?¶
Imagine you have a software installed on Windows that automatically remembers its installation path and the locations of commonly used tools. In Linux, environment variables act like this “system memory”—they are variables that store data needed for system or program operations, such as software installation paths, user information, language settings, and more.
In simple terms, environment variables are configurations the system “remembers,” so you don’t have to repeatedly input complex information. For example, when running Python, the system can automatically locate Python’s executable file without you specifying the full path each time.
2. Why Set Environment Variables?¶
The most common use case is: to help the system find the programs you want to run. For instance, if you wrote a custom script (e.g., test.sh), you might want to run it from any directory without typing the full path (e.g., ./test.sh). By adding the script’s directory to the PATH environment variable, you can simply type test.sh to execute it.
Other examples:
- PATH: A list of directories the system searches for executable files (e.g., where ls or python are located).
- LANG: Controls the system’s language (e.g., setting LANG=zh_CN.UTF-8 can resolve Chinese garbled text).
- HOME: Points to your home directory (e.g., /home/yourname, represented by ~).
3. How to View Environment Variables¶
Use the following commands to quickly check environment variables:
1. View a Single Variable¶
echo $VARIABLE_NAME
For example, to check the current directory:
echo $PWD
To check the current username:
echo $USER
2. View All Environment Variables¶
- Use
env: Lists all environment variables (without arguments). - Use
printenv: Similar toenv, it also lists all variables. You can specify a variable name (e.g.,printenv PATH).
4. Temporary Environment Variables (Valid Only for the Current Terminal)¶
Temporary settings only take effect in the currently open terminal window and expire when the terminal is closed. Use the export command:
# Set a variable (format: export VAR_NAME=VALUE)
export MY_VAR="hello"
export PATH=$PATH:/new/path # Append a new path to PATH (explained in detail later)
After setting, verify with echo $MY_VAR:
echo $MY_VAR # Output: hello
5. Permanent Environment Variables (Persist After Restart)¶
Temporary settings only work for the current terminal. To make variables persist (e.g., available in every new terminal), modify configuration files. Linux has two levels of configuration files:
1. User-Level Configuration (Applies Only to the Current User)¶
~/.bashrc: Executed automatically when a new terminal (bash) is opened. Ideal for user-specific configurations.~/.profile: Executed by some shells (e.g., bash) during startup. Also suitable for user configurations.
To modify:
# Open the configuration file (using nano or vim)
nano ~/.bashrc # or vim ~/.bashrc
# Add the following line at the end (no spaces around the equals sign)
export MY_VAR="hello"
# Apply changes (critical step!)
source ~/.bashrc # or source ~/.profile
2. System-Level Configuration (Applies to All Users, Requires sudo)¶
/etc/profile: Executed by all users upon login (system-wide global config)./etc/environment: System-wide environment variables (usually modified indirectly via/etc/profile.d/*.shscripts).
Modify system-level files with sudo:
sudo nano /etc/profile # or other system-level files
6. The Most Important Environment Variable: PATH¶
PATH is the system’s list of directories for searching executable files, formatted like:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
When you run a command, the system checks each directory in PATH sequentially. To run a custom script (e.g., in ~/scripts from any directory), add the script’s directory to PATH:
# Temporary addition to PATH
export PATH=$PATH:~/scripts
# Permanent addition to ~/.bashrc
echo 'export PATH=$PATH:~/scripts' >> ~/.bashrc
source ~/.bashrc # Apply changes
Now you can run your_script.sh from any directory!
7. Common Environment Variables (Quick Reference)¶
| Variable | Purpose | Example |
|---|---|---|
PATH |
Directories the system searches for executables | Includes /usr/bin (system commands), /home/user/bin (user scripts), etc. |
HOME |
Current user’s home directory | /home/yourname |
USER |
Current username | yourname |
LANG |
System language/character encoding | LANG=zh_CN.UTF-8 (for Chinese) |
PS1 |
Terminal prompt format | \[\e]0;\u@\h:\w\a\]\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ (custom prompt) |
8. Important Notes¶
- Temporary vs. Permanent: Use
exportfor temporary settings, and modify configuration files for permanent ones. - Permission Issues: Modify system-level files (e.g.,
/etc/profile) withsudoto avoid accidental system changes. - Values with Spaces: Enclose values in quotes (e.g.,
export MY_VAR="hello world"). - 生效方式: After modifying a config file, run
source filenameor restart the terminal to apply changes.
Summary¶
Environment variables are critical for Linux to “remember” configurations. Mastering their setup and usage will significantly improve your efficiency. Focus on PATH (for global program access) and configuration file management to flexibly control your system environment. Practice with echo $VARIABLE and env to quickly familiarize yourself with their roles!