Environment Variables and .bashrc

intermediate
12 min

Environment Variables and .bashrc

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.

Reading Environment Variables

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:

bash
printenv | less # list all environment variables echo $HOME # /home/yourname echo $USER # your username echo $SHELL # /bin/bash echo $PATH # directories searched for commands

By convention, environment variable names are UPPERCASE. The dollar sign is only used when reading a variable, never when assigning it.

Shell Variables vs Environment Variables

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:

bash
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 exported

Many programs read specific variables: git and crontab respect EDITOR, and countless tools respect LANG and TZ for locale and timezone.

Understanding PATH

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:

bash
export PATH="$HOME/bin:$PATH" which ls # show which file will be executed

Always 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.

Making Changes Permanent with .bashrc

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:

bash
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 ~/.bashrc

The .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.

Practical Example: Project Configuration

Applications commonly read secrets and settings from the environment instead of hard-coded files. You can test an app locally like this:

bash
export DATABASE_URL="postgres://localhost:5432/devdb" export APP_ENV=development python app.py

This pattern, known as twelve-factor configuration, keeps credentials out of your source code and lets the same code run unchanged in development and production.

Key Takeaways

  • Environment variables are inherited key-value pairs; read them with echo $NAME and printenv.
  • Use export to make a variable visible to child processes.
  • PATH controls where the shell looks for commands; always preserve the old value when editing it.
  • Put permanent exports and aliases in ~/.bashrc and reload with source ~/.bashrc.
  • Storing configuration in the environment keeps secrets out of your code.

Next up: Shell Scripting Basics, where you will combine variables, commands and logic into reusable scripts that automate real work.

Environment Variables and .bashrc - Linux & Command Line | CodeYourCraft | CodeYourCraft