Environment Variables and PATH in Linux
Environment variables are dynamic values used by the shell and programs to configure behavior. They are stored as key-value pairs and affect how processes run.
Common Environment Variables
| Variable | Description |
|---|---|
PATH | Directories to search for executable commands |
HOME | Path to the current user’s home directory |
USER | Username of the current user |
SHELL | Default shell used by the user |
PWD | Present working directory |
EDITOR | Default text editor |
LANG | System language/locale settings |
Viewing Environment Variables
printenv # List all environment variables
echo $HOME # Show value of a specific variable
env # Another way to list current environmentSetting Environment Variables
Temporary (for current session only):
VARIABLE_NAME=valueExample:
MY_VAR=hello
echo $MY_VAR # Output: helloExporting (to make it available to subprocesses):
export VARIABLE_NAME=valueExample:
export MY_NAME="Alice"Making Variables Permanent
To persist variables across sessions, define them in:
~/.bashrcor~/.bash_profile(for Bash)~/.zshrc(for Zsh)
Example:
export MY_NAME="Alice"Then reload:
source ~/.bashrcThe PATH Variable
What is PATH?
PATH is an environment variable that tells the shell where to look for executable programs.
echo $PATHTypical output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binAdding a Directory to PATH Temporarily:
export PATH=$PATH:/your/custom/pathExample:
export PATH=$PATH:/opt/myapp/binMaking PATH Change Permanent:
Add to your shell config file:
echo 'export PATH=$PATH:/opt/myapp/bin' >> ~/.bashrc
source ~/.bashrcBest Practices
- Use
exportfor variables needed by subprocesses. - Avoid overwriting
PATHentirely; always append or prepend. - Use lowercase for custom temporary variables to avoid conflicts (e.g.,
my_var).
Last updated on