Environment Variables
PATH, export, .bashrc
What are Environment Variables?
Environment variables are key-value pairs that affect process behavior:
- Tell programs where to find things
- Configure program behavior
- Pass information between processes
Viewing Variables
printenv # All environment variables
printenv PATH # Specific variable
echo $PATH # Also works
echo $HOME
echo $USER
env # Same as printenv
Important Variables
| Variable | Purpose | Example |
|----------|---------|---------|
| PATH | Where to find commands | /usr/bin:/bin |
| HOME | Your home directory | /home/alice |
| USER | Your username | alice |
| SHELL | Your shell | /bin/bash |
| EDITOR | Default text editor | vim or nano |
| LANG | Language/locale | en_US.UTF-8 |
| PWD | Current directory | /home/alice/project |
| TERM | Terminal type | xterm-256color |
Setting Variables
For Current Session
MY_VAR="hello" # Set (not exported)
export MY_VAR="hello" # Set and export to children
export PATH="$PATH:/new/path" # Add to existing PATH
Exported vs Non-Exported
PRIVATE="secret" # Only this shell sees it
export PUBLIC="visible" # Child processes see it too
# Test:
echo $PRIVATE # Works in current shell
bash -c 'echo $PRIVATE' # Empty (not exported)
bash -c 'echo $PUBLIC' # "visible" (exported)
Making Variables Permanent
~/.bashrc — Interactive Non-Login Shells
# Add to ~/.bashrc
export MY_VAR="permanent"
export PATH="$PATH:$HOME/bin"
alias ll='ls -la'
~/.profile or ~/.bash_profile — Login Shells
# Add to ~/.profile
export EDITOR=vim
Reload Configuration
source ~/.bashrc # Reload bashrc
. ~/.profile # Shorthand for source
The PATH Variable
PATH tells the shell where to find commands:
echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin
which python # Shows /usr/bin/python
which ls # Shows /usr/bin/ls
Adding to PATH
# Temporarily
export PATH="$PATH:/opt/myapp/bin"
# Permanently (add to ~/.bashrc)
export PATH="$HOME/.local/bin:$PATH" # Prepend (higher priority)
export PATH="$PATH:/opt/tools" # Append
Custom Scripts
mkdir -p ~/bin
# Put scripts there, add to PATH:
export PATH="$HOME/bin:$PATH"
Unsetting Variables
unset MY_VAR # Remove variable
Startup File Order
When you log in:
/etc/profile(system-wide)~/.bash_profileOR~/.bash_loginOR~/.profile
When you open a terminal:
/etc/bash.bashrc(system-wide)~/.bashrc
Best Practice
Put everything in ~/.bashrc, and have ~/.profile source it:
# In ~/.profile
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
Common Customizations
Add to ~/.bashrc:
# Aliases
alias ll='ls -la'
alias la='ls -A'
alias ..='cd ..'
alias ...='cd ../..'
alias grep='grep --color=auto'
# Custom prompt
export PS1='\u@\h:\w\$ '
# Editor
export EDITOR=vim
export VISUAL=vim
# History settings
export HISTSIZE=10000
export HISTFILESIZE=20000
export HISTCONTROL=ignoredups:erasedups
Try It!
Practice environment variables in the terminal:
Exercises:
- View your PATH:
echo $PATH - Create a variable:
export MY_NAME="Ubuntu User" - Use it:
echo "Hello, $MY_NAME" - View all variables:
printenv | head -20
Try it in the terminal
A sandboxed shell with this lesson’s commands. Type help to see what’s available.