Environment Variables
What are Environment Variables?
Environment variables are dynamic values stored in the operating system that affect program behavior and how the system runs.
Viewing Environment Variables
bash
# View all environment variables
$ env
$ printenv
# View specific variable
$ echo $PATH
$ printenv PATH
# Use set to view all variables (including Shell variables)
$ setCommon Environment Variables
| Variable | Description |
|---|---|
PATH | Executable file search paths |
HOME | User home directory |
USER | Current username |
SHELL | Default shell |
PWD | Current working directory |
OLDPWD | Previous working directory |
LANG | Language setting |
TERM | Terminal type |
EDITER | Default editor |
HOSTNAME | Hostname |
PS1 | Command prompt |
LD_LIBRARY_PATH | Shared library search path |
Setting Environment Variables
Temporary Setting
bash
# Set variable (only current Shell)
$ export MYVAR="value"
# Set variable and run command
$ MYVAR="value" command
# Unset variable
$ unset MYVARPermanent Setting
User-level Configuration
Edit ~/.bashrc or ~/.profile:
bash
# ~/.bashrc
export MYVAR="value"
export PATH="$PATH:/new/path"
export EDITOR="vim"bash
# Make configuration effective
$ source ~/.bashrcSystem-level Configuration
bash
# /etc/environment (recommended)
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin:/sbin:/bin"
LANG="en_US.UTF-8"
# /etc/profile (all users at login)
export JAVA_HOME="/usr/lib/jvm/java-11"
# /etc/profile.d/*.sh
# Create independent configuration files
$ sudo vim /etc/profile.d/myapp.sh
export MYAPP_HOME="/opt/myapp"PATH Variable
PATH is one of the most important environment variables, defining the directories that Shell searches for commands.
Viewing PATH
bash
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin
# Display one path per line
$ echo $PATH | tr ':' '\n'Modifying PATH
bash
# Add to end
$ export PATH="$PATH:/new/path"
# Add to beginning (priority search first)
$ export PATH="/new/path:$PATH"
# Permanent add (in ~/.bashrc)
export PATH="$PATH:$HOME/bin"
export PATH="$PATH:$HOME/.local/bin"PATH Search Order
bash
# View command location
$ which python
/usr/bin/python
# View all matches
$ which -a python
/usr/bin/python
/usr/local/bin/pythonLanguage and Region Settings
bash
# View current settings
$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
...
# Set language
export LANG="zh_CN.UTF-8"
# Set all regions
export LC_ALL="en_US.UTF-8"
# View available languages
$ locale -aCommon Configuration Examples
Development Environment
bash
# ~/.bashrc
# Java
export JAVA_HOME="/usr/lib/jvm/java-17"
export PATH="$PATH:$JAVA_HOME/bin"
# Go
export GOPATH="$HOME/go"
export PATH="$PATH:$GOPATH/bin"
# Node.js
export NVM_DIR="$HOME/.nvm"
# Python
export PYTHONPATH="$HOME/python/lib"
# Editor
export EDITOR="vim"
export VISUAL="vim"Proxy Settings
bash
export http_proxy="http://proxy.example.com:8080"
export https_proxy="http://proxy.example.com:8080"
export no_proxy="localhost,127.0.0.1,.example.com"
# Unset proxy
unset http_proxy https_proxy no_proxyShell Variables vs Environment Variables
bash
# Shell variable (only current Shell)
myvar="value"
# Environment variable (passes to subprocesses)
export myvar="value"
# Verify
$ myvar="shell_var"
$ bash -c 'echo $myvar' # empty
$ export myvar="env_var"
$ bash -c 'echo $myvar' # env_varSummary
This chapter introduced environment variables:
- Viewing variables:
env,printenv,echo - Setting variables:
export, configuration files - PATH variable: Command search paths
- Configuration files:
~/.bashrc,/etc/environment - Language settings: locale
- Configuration examples: Development, proxy settings
Environment variables are an important way to configure系统和应用程序。Mastering them will greatly improve your efficiency.
Previous chapter: Shell Scripting Basics
Next chapter: Practical Examples