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 $USEROutput:
/home/asha
ashaImportant Built-In Variables
| Variable | Meaning |
|---|---|
| HOME | Path of the user home folder |
| USER | Name of the logged-in user |
| PWD | Current working folder |
| PATH | Folders searched for commands |
| SHELL | Path of the default shell |
| HOSTNAME | Name of the computer |
| LANG | Language and character setting |
Create Your Own Environment Variable
Set and Export in One Line
export APP_MODE="testing"
echo $APP_MODESet First, Export Later
APP_NAME="Inventory"
export APP_NAMESet 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 $GREETINGOutput:
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.shOutput:
City:
Team: BlueThe 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 $PATHKeep 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
~/.bashrcloads for every new interactive terminal window.~/.bash_profileloads once when you log in./etc/environmentsets values for all users and needs administrator rights.
echo 'export APP_MODE="production"' >> ~/.bashrc
source ~/.bashrcThe 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
exportto share a variable with child processes. - Use
printenvto list values andunsetto remove one. - Save exports inside
~/.bashrcfor permanent effect. - Append to
PATHand never overwrite it.
