Skip to content

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)
$ set

Common Environment Variables

VariableDescription
PATHExecutable file search paths
HOMEUser home directory
USERCurrent username
SHELLDefault shell
PWDCurrent working directory
OLDPWDPrevious working directory
LANGLanguage setting
TERMTerminal type
EDITERDefault editor
HOSTNAMEHostname
PS1Command prompt
LD_LIBRARY_PATHShared 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 MYVAR

Permanent 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 ~/.bashrc

System-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/python

Language 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 -a

Common 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_proxy

Shell 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_var

Summary

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

Content is for learning and research only.