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.

Create a free account to save this page, take notes and track your progress. Sign in

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.

export and one-off variables
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".

Adding a directory to 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?

  1. Saves the variable to disk
  2. Makes a variable available to child processes
  3. Prints the variable
  4. Makes it read-only
Show answer

Answer: Makes a variable available to child processes

Was this page helpful?