Bash Environment Variables

An environment variable stores a value that every program in a terminal session can read. The system uses them to remember your username, home folder, language, and many other settings. Scripts read these values to adapt to each computer.

Shell Variables and Environment Variables

A normal variable lives only inside the current shell. An environment variable travels with every program that the shell launches. The export command turns a normal variable into an environment variable.

+----------------------------------------------------+
|  Terminal Session (parent shell)                   |
|                                                    |
|   city="Pune"           (shell variable)           |
|   export TEAM="Blue"    (environment variable)     |
|                                                    |
|        |                                           |
|        v  starts a script                          |
|  +----------------------------------------------+  |
|  |  Child Script                                |  |
|  |   city -> empty    (not inherited)           |  |
|  |   TEAM -> "Blue"   (inherited)               |  |
|  +----------------------------------------------+  |
+----------------------------------------------------+

View Existing Environment Variables

The printenv command lists every variable. Add a name to see one value.

printenv
printenv HOME
echo $USER

Output:

/home/asha
asha

Important Built-In Variables

VariableMeaning
HOMEPath of the user home folder
USERName of the logged-in user
PWDCurrent working folder
PATHFolders searched for commands
SHELLPath of the default shell
HOSTNAMEName of the computer
LANGLanguage and character setting

Create Your Own Environment Variable

Set and Export in One Line

export APP_MODE="testing"
echo $APP_MODE

Set First, Export Later

APP_NAME="Inventory"
export APP_NAME

Set a Value for One Command Only

Place the assignment before a command. The value disappears when the command ends.

GREETING="Hi" bash -c 'echo $GREETING'
echo $GREETING

Output:

Hi

The second line prints nothing because the value existed only during the first command.

Prove That Child Scripts Inherit Values

Create a file named show.sh:

#!/bin/bash
echo "City: $city"
echo "Team: $TEAM"

Run the commands below in the terminal:

city="Pune"
export TEAM="Blue"
bash show.sh

Output:

City:
Team: Blue

The script prints nothing for city because the variable was never exported.

Remove a Variable

unset APP_MODE
echo "Mode is: $APP_MODE"

The output shows an empty value after unset runs.

Change the PATH Variable

The PATH variable holds folder names separated by colons. Add a new folder at the end to run your own tools from anywhere.

export PATH="$PATH:$HOME/tools"
echo $PATH

Keep the old value inside the quotes. Writing export PATH="$HOME/tools" alone erases the original list and breaks basic commands such as ls until you restart the terminal.

Make Variables Permanent

A variable set in the terminal disappears when the window closes. Save the line inside a startup file to keep it.

Startup Files

  • ~/.bashrc loads for every new interactive terminal window.
  • ~/.bash_profile loads once when you log in.
  • /etc/environment sets values for all users and needs administrator rights.
echo 'export APP_MODE="production"' >> ~/.bashrc
source ~/.bashrc

The source command reloads the file without closing the terminal.

Use a Default Value

A script can fall back to a default when a variable is missing:

#!/bin/bash
mode=${APP_MODE:-development}
echo "Running in $mode mode"

The script prints development when APP_MODE is empty or unset.

Keep Secrets Safe

Environment variables often hold passwords and access keys. Avoid typing secrets directly into scripts that you share or upload. Load them from a private file that only your account can read.

Key Takeaways

  • Environment variables pass settings from a shell to the programs it starts.
  • Use export to share a variable with child processes.
  • Use printenv to list values and unset to remove one.
  • Save exports inside ~/.bashrc for permanent effect.
  • Append to PATH and never overwrite it.