Environment Variables
$PATH, export, .bashrc, .profile — making the shell work for you
Skills taught: linux.env-vars, linux.path, linux.export, linux.bashrc, linux.dotfiles
Environment variables
# Read a variable
echo $HOME # /home/alice
echo $USER # alice
echo $SHELL # /bin/bash
echo $PATH # /usr/local/bin:/usr/bin:/bin:...
# Set a variable (only current shell session)
NAME="Alice"
echo $NAME # Alice
# export: make a variable available to child processes
export DATABASE_URL="postgresql://localhost/myapp"
export NODE_ENV="development"
# View all environment variables
env
printenv
printenv PATH # print just one variable
# Unset a variable
unset DATABASE_URL
# Set for a single command
NODE_ENV=production node app.js # only sets for that command
$PATH — how the shell finds commands
# PATH is a colon-separated list of directories
echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/home/alice/.local/bin
# When you type 'node', the shell searches each PATH dir in order
# and runs the first 'node' it finds
which node # /usr/local/bin/node
# Add a directory to PATH
export PATH="$HOME/.local/bin:$PATH" # prepend (higher priority)
export PATH="$PATH:/opt/myapp/bin" # append (lower priority)
# Common additions to PATH:
# ~/.local/bin — user-installed tools
# ~/.cargo/bin — Rust (cargo install)
# ~/go/bin — Go binaries
# /opt/homebrew/bin — macOS Homebrew (Apple Silicon)
.bashrc and .profile — persisting configuration
# .bashrc — loaded for every interactive non-login bash shell
# Put: aliases, functions, PATH additions, prompt customization
# .bash_profile / .profile — loaded for login shells
# Put: environment variables, PATH (loaded once at login)
# .zshrc — same as .bashrc but for zsh
# Edit your .bashrc:
nano ~/.bashrc
# Add:
export PATH="$HOME/.local/bin:$PATH"
export EDITOR="vim"
alias ll='ls -la'
alias gs='git status'
alias gp='git push'
# Reload without opening a new terminal
source ~/.bashrc # or: . ~/.bashrc
Tip: Put export statements (env vars, PATH) in ~/.profile or ~/.bash_profile so they load once at login. Put aliases and functions in ~/.bashrc for interactive sessions. Both files are loaded in most setups — when in doubt, use ~/.bashrc.
Frequently Asked Questions
Where should I store sensitive environment variables (API keys)?
Not in .bashrc — that file gets committed to git by accident. Use a .env file in your project (and add .env to .gitignore), then load it with a tool like direnv (automatic per-directory env) or explicitly with export $(cat .env | xargs) in your session. For production, use your hosting platform's secret management (Heroku config vars, Railway variables, Kubernetes secrets).