Environment & PATH

Environment variables configure your session; PATH tells the shell where to find commands.

Syntaxexport NAME=value echo $PATH

Environment variables are named values available to every program in your session, such as HOME, USER and PATH.

Viewing and setting

  • printenv or env — list all environment variables.
  • echo "$HOME" — print one.
  • export NAME=value — set one for this session and its child programs.

PATH — how commands are found

PATH is a colon-separated list of directories. When you type a command, the shell searches these folders in order for a matching program. That is why ls works from anywhere — /usr/bin is on your PATH.

Example

Example · bash
# Show your PATH
echo $PATH
# /usr/local/bin:/usr/bin:/bin:/home/alice/.local/bin

# Set a variable for this session
export EDITOR=nano

# Add a folder to PATH (put in ~/.bashrc to keep it)
export PATH="$HOME/bin:$PATH"

# List every environment variable
printenv

When to use it

  • A developer prepends '$HOME/.local/bin' to PATH in ~/.bashrc so user-installed Python tools are found before system ones.
  • A CI pipeline sets 'export APP_ENV=production' before running the app to configure environment-specific behaviour.
  • A sysadmin uses 'env -i /bin/bash' to run a script in a clean environment to reproduce a reported 'command not found' error.

More examples

Inspect and print environment

Shows three ways to inspect environment variables: env for all, echo for one, and printenv for one.

Example · bash
# Print all environment variables
env

# Print specific variable
echo $PATH
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# Print a variable with printenv
printenv HOME
# /home/alice

Set and export variables

Contrasts plain assignment (local to shell) with export (available to child processes and subshells).

Example · bash
# Set a variable (only in current shell)
MY_VAR=hello

# Export so child processes inherit it
export MY_VAR=hello
export APP_ENV=production

# Verify
printenv APP_ENV
# production

Add directory to PATH

Prepends a directory to PATH and persists the change in ~/.bashrc so it survives new terminal sessions.

Example · bash
# Add user's local bin to start of PATH
export PATH="$HOME/.local/bin:$PATH"

# Persist across sessions
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc

# Verify
which my-tool
# /home/alice/.local/bin/my-tool

Discussion

  • Be the first to comment on this lesson.
Environment & PATH — Linux | SoundsCode