Bash Tutorial · Linux System Skills · Lesson 26 of 30
Bash Environment Variables and PATH
Use environment variables, export them to child processes, configure PATH, and set up your shell profile.
Shell variables vs environment variables
A plain variable exists only in the current shell. export puts it in the environment, which child
processes (scripts, programs) inherit.
greeting="hi"
bash -c 'echo "child sees: [${greeting:-nothing}]"'
export greeting
bash -c 'echo "child sees: [$greeting]"'
LOG_LEVEL=debug bash -c 'echo "one-off variable: $LOG_LEVEL"'
Output
child sees: [nothing] child sees: [hi] one-off variable: debug
Run this on your computer — see “Get Started” for setup.
PATH
PATH is a colon-separated list of directories searched for commands. Add your own tools directory to the
front: export PATH="$HOME/bin:$PATH".
mkdir -p "$HOME/bin"
printf '#!/usr/bin/env bash\necho "my tool runs"\n' > "$HOME/bin/mytool"
chmod +x "$HOME/bin/mytool"
command -v mytool || echo "mytool not found"
export PATH="$HOME/bin:$PATH"
mytool
Output
mytool not found my tool runs
Run this on your computer — see “Get Started” for setup.
Making it permanent
Put exports in ~/.bashrc (interactive shells) or ~/.profile/~/.bash_profile (login shells), then
source ~/.bashrc to reload.
Test yourself
What does export do?
Saves the variable to diskMakes a variable available to child processesPrints the variableMakes it read-only
Show answer
Answer: Makes a variable available to child processes