TExcelTAD
Home › Linux & Bash › Environment Variables & PATH

Environment Variables & PATH

Every process carries a set of environment variables: name/value pairs it inherits from the process that started it. They tell programs where your home is (HOME), which language to use (LANG), which editor you like (EDITOR) and, most importantly, where to look for commands (PATH).

Shell variables vs environment variables

A normal variable belongs to the current shell only. export marks it for the environment, so child processes (scripts, programs) receive a copy:

bash · try it yourself
app_mode="debug"
bash -c 'echo "child sees app_mode=[$app_mode]"'
export app_mode
bash -c 'echo "child sees app_mode=[$app_mode]"'
export DB_HOST="db.internal" DB_PORT=5432
bash -c 'echo "child sees $DB_HOST:$DB_PORT"'
child sees app_mode=[]
child sees app_mode=[debug]
child sees db.internal:5432

The copy is one-way: a child can change its own environment, but never its parent's. That's why a script can't export a variable into your terminal; you have to source it instead.

Looking at the environment

Command Shows
env / printenv All environment variables
printenv HOME One variable
echo "$HOME" One variable (expanded by the shell)
declare -x Exported variables, Bash style
set All shell variables and functions

The full environment is long and machine-specific, so filter it:

bash · try it yourself
export APP_NAME=shop APP_PORT=8080 APP_DEBUG=0
env | grep '^APP_' | sort
printenv APP_PORT
echo "home=$HOME lang=$LANG tz=$TZ"
APP_DEBUG=0
APP_NAME=shop
APP_PORT=8080
8080
home=/home/learner lang=C.UTF-8 tz=UTC

Important standard variables

Variable Meaning
HOME Your home directory (~)
USER / LOGNAME Your user name (set by the login process)
PWD / OLDPWD Current / previous directory
PATH Directories searched for commands
SHELL Your login shell
LANG, LC_* Language and formatting (locale)
TZ Time zone
EDITOR, PAGER Preferred editor and pager
PS1 The prompt's format (interactive shells)

Setting a variable for one command

NAME=value command puts a variable into the environment of that single command, without changing your shell:

bash · try it yourself
TZ=Asia/Tokyo date -d '2026-03-15 12:00 UTC' '+%H:%M %Z'
TZ=America/New_York date -d '2026-03-15 12:00 UTC' '+%H:%M %Z'
date -d '2026-03-15 12:00 UTC' '+%H:%M %Z'
LANG=C.UTF-8 GREETING=hi bash -c 'echo "$GREETING from a child"'
echo "GREETING here: [${GREETING-}]"
21:00 JST
08:00 EDT
12:00 UTC
hi from a child
GREETING here: []

PATH: how Bash finds commands

When you type ls, Bash searches each directory in PATH, left to right, and runs the first executable named ls it finds. PATH is a colon-separated list:

bash · try it yourself
echo "$PATH" | tr ':' '\n'
command -v ls bash
type -a echo
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/bin/ls
/bin/bash
echo is a shell builtin
echo is /bin/echo

command -v prints the path that would be used (or nothing if the command doesn't exist), which is the reliable way to check for a tool in scripts. type -a shows every match, including built-ins.

Adding your own commands

Put your scripts in a directory such as ~/bin or ~/.local/bin and add it to PATH. Then you can run them by name from anywhere:

bash · try it yourself
mkdir -p ~/bin
cat > ~/bin/hello-tool <<'SCRIPT'
#!/usr/bin/env bash
echo "hello-tool running from $0"
SCRIPT
chmod +x ~/bin/hello-tool
hello-tool
export PATH="$HOME/bin:$PATH"
hello-tool
command -v hello-tool
/w/main.sh: line 7: hello-tool: command not found
hello-tool running from /home/learner/bin/hello-tool
/home/learner/bin/hello-tool

To make the change permanent, add export PATH="$HOME/bin:$PATH" to ~/.bashrc (Shell Configuration lesson). Putting your directory first lets your scripts override system commands; putting it last ("$PATH:$HOME/bin") is safer.

Warning: Never put . (the current directory) in PATH. Anyone who can write a file named ls into a directory you visit could run code as you.

Checking for required tools

bash · try it yourself
require() {
  local tool
  for tool in "$@"; do
    command -v "$tool" > /dev/null || { echo "missing required tool: $tool" >&2; return 1; }
  done
  echo "all tools present: $*"
}
require tar gzip jq
require tar terraform || echo "install it first"
all tools present: tar gzip jq
missing required tool: terraform
install it first

Configuration through the environment

The "twelve-factor app" style configures programs through environment variables: DATABASE_URL, PORT, LOG_LEVEL. Tools like Docker and systemd let you set them per service, and many projects keep them in a .env file. Bash can load such a file with set -a (auto-export) and source:

bash · try it yourself
cat > .env <<'CONF'
DATABASE_URL=postgres://app@db:5432/shop
LOG_LEVEL=info
CONF
set -a
source .env
set +a
bash -c 'echo "child got LOG_LEVEL=$LOG_LEVEL and ${DATABASE_URL%%:*} database"'
child got LOG_LEVEL=info and postgres database

Note: Keep .env files out of Git (add them to .gitignore): they usually contain passwords and API keys.

Test yourself

What does export do?

  1. Saves a variable to a file
  2. Makes a variable available to child processes
  3. Prints all variables
  4. Deletes a variable

How does Bash find the program for a command like git?

  1. It searches the whole disk
  2. It searches the directories in $PATH from left to right
  3. It looks in the current directory first
  4. It asks the package manager

Which command runs make with CC=clang without changing your shell's variables?

  1. export CC=clang; make
  2. CC=clang make
  3. make --env CC=clang
  4. set CC=clang make