Every process on a Linux system starts with a small dictionary of key-value pairs called environment variables. They tell programs where to find executables, which editor you prefer, what language to display, and much more. In this lesson you will learn how to read and set environment variables, how the PATH variable works, and how to make your customizations permanent using the .bashrc file.
To see the environment your shell is running with, use env or printenv. To read a single variable, prefix its name with a dollar sign and print it with echo:
printenv | less # list all environment variables
echo $HOME # /home/yourname
echo $USER # your username
echo $SHELL # /bin/bash
echo $PATH # directories searched for commandsBy convention, environment variable names are UPPERCASE. The dollar sign is only used when reading a variable, never when assigning it.
There is an important distinction. A plain assignment creates a shell variable that only the current shell can see. Adding export promotes it to an environment variable, which is inherited by every child process the shell launches:
GREETING=hello # shell variable, invisible to child processes
export GREETING # now child processes inherit it
export EDITOR=nano # assign and export in one step
bash -c 'echo $GREETING' # prints hello, because it was exportedMany programs read specific variables: git and crontab respect EDITOR, and countless tools respect LANG and TZ for locale and timezone.
When you type a command, the shell searches each directory listed in PATH, in order, and runs the first match. That is why typing ls works without typing /usr/bin/ls. To add your own scripts directory to the search path, prepend or append it:
export PATH="$HOME/bin:$PATH"
which ls # show which file will be executedAlways include the existing $PATH in the new value. Overwriting PATH entirely will make basic commands like ls and cat unfindable until you close the terminal.
Variables set at the prompt disappear when the terminal closes. To persist them, add the export lines to ~/.bashrc, a script that Bash runs every time an interactive shell starts:
nano ~/.bashrc
# add lines like these at the bottom:
export EDITOR=nano
export PATH="$HOME/bin:$PATH"
alias ll='ls -alF'
# reload without opening a new terminal:
source ~/.bashrcThe .bashrc file is also the traditional home for aliases and shell functions. On login shells (for example SSH sessions) Bash reads ~/.profile or ~/.bash_profile first; most distributions make that file source .bashrc, so putting your customizations in .bashrc covers both cases.
Applications commonly read secrets and settings from the environment instead of hard-coded files. You can test an app locally like this:
export DATABASE_URL="postgres://localhost:5432/devdb"
export APP_ENV=development
python app.pyThis pattern, known as twelve-factor configuration, keeps credentials out of your source code and lets the same code run unchanged in development and production.
Next up: Shell Scripting Basics, where you will combine variables, commands and logic into reusable scripts that automate real work.