Environment Variables & PATH
Learn how Linux stores environment information, how shells discover commands, and why environment variables are fundamental to modern development and DevOps workflows.
Difficulty: Beginner
Estimated reading time: 45 min
Introduction
Environment variables are one of the most important concepts in Linux.
They are everywhere.
You already use them constantly — often without realizing it.
Environment variables influence:
- command execution
- application behavior
- shell configuration
- development environments
- Docker containers
- CI/CD pipelines
- cloud infrastructure
Understanding environment variables is critical for:
- Linux administration
- development
- Docker
- Kubernetes
- automation
- shell scripting
One of the most famous environment variables is:
PATH
Without it, Linux would not know how to locate commands like:
ls
docker
git
python
What Is an Environment Variable?
An environment variable is:
A named value stored inside the shell or process environment.
Example:
HOME=/home/john
Variables allow processes to store and share configuration information.
Think of them as:
Global settings for applications and shells.
Environment Variables Everywhere
Examples of common variables:
| Variable | Purpose |
|---|---|
HOME |
User home directory |
USER |
Current username |
PWD |
Current directory |
PATH |
Executable search paths |
SHELL |
Current shell |
HOSTNAME |
Machine hostname |
Viewing Environment Variables
Display all environment variables:
env
or:
printenv
Example output:
HOME=/home/john
USER=john
PATH=/usr/local/bin:/usr/bin:/bin
Linux processes inherit many variables automatically.
Viewing Specific Variables
Example:
echo $HOME
Output:
/home/john
The $ symbol expands variable values.
More Examples
Current user:
echo $USER
Current shell:
echo $SHELL
Current working directory:
echo $PWD
Understanding Variable Expansion
When Bash sees:
$HOME
it replaces it with the variable value.
Example:
cd $HOME
becomes:
cd /home/john
internally.
Creating Variables
Temporary variable:
MY_NAME="John"
Access it:
echo $MY_NAME
Output:
John
Local Variables vs Environment Variables
This distinction is important.
Local Shell Variable
MY_VAR="hello"
Exists only inside the current shell.
Child processes cannot access it.
Exported Environment Variable
export MY_VAR="hello"
Now child processes inherit the variable.
This is why the command is called:
export
Why Export Matters
Example:
MY_VAR="hello"
python app.py
The Python process may NOT see MY_VAR.
But:
export MY_VAR="hello"
python app.py
Now the child process inherits the variable.
This becomes extremely important in:
- Docker
- CI/CD
- cloud infrastructure
- scripting
Understanding PATH
What Is PATH?
PATH is one of the most important environment variables in Linux.
Example:
echo $PATH
Output:
/usr/local/bin:/usr/bin:/bin
PATH contains:
Directories Linux searches for executables.
How Command Resolution Works
Imagine typing:
docker
The shell searches directories listed in PATH:
/usr/local/bin
/usr/bin
/bin
until it finds:
/usr/bin/docker
Then Linux executes it.
Why PATH Exists
Without PATH, you would need full executable paths:
/usr/bin/docker
/usr/bin/git
/bin/ls
PATH allows simpler commands:
docker
git
ls
This dramatically improves usability.
Understanding PATH Order
Order matters.
Example:
/usr/local/bin:/usr/bin:/bin
Linux searches from left to right.
The first matching executable wins.
This can become important when multiple versions exist.
Example: Multiple Python Versions
Imagine:
/usr/bin/python
/usr/local/bin/python
If /usr/local/bin appears first in PATH:
python
executes:
/usr/local/bin/python
This behavior becomes very important in development environments.
Locating Executables
Useful command:
which python
Example:
/usr/bin/python
This shows which executable PATH resolved.
Another Useful Command
whereis python
This searches broader filesystem locations.
Adding Directories to PATH
Temporary example:
export PATH=$PATH:/home/john/bin
Breakdown:
| Part | Meaning |
|---|---|
$PATH |
Existing PATH |
: |
Separator |
/home/john/bin |
New directory |
Now executables inside:
/home/john/bin
can run directly.
Example
File:
/home/john/bin/hello
Make executable:
chmod +x hello
Now run directly:
hello
without specifying the full path.
Temporary vs Persistent Variables
Variables created in the shell disappear after logout.
Example:
export MY_VAR="hello"
After closing the terminal:
Variable disappears
Persistent Shell Configuration
To make variables permanent, add them to shell config files.
Common files:
| File | Purpose |
|---|---|
~/.bashrc |
Interactive Bash shell |
~/.profile |
Login shell config |
~/.zshrc |
Zsh config |
Example: Persistent PATH
Edit:
nano ~/.bashrc
Add:
export PATH=$PATH:/home/john/bin
Reload configuration:
source ~/.bashrc
Now PATH changes persist across sessions.
What Does source Do?
Example:
source ~/.bashrc
This reloads the file inside the current shell.
Without source, you usually need to reopen the terminal.
Environment Variables in Applications
Applications constantly use environment variables.
Examples:
| Variable | Purpose |
|---|---|
NODE_ENV |
Node.js environment |
PORT |
Server port |
DATABASE_URL |
Database connection |
API_KEY |
Authentication |
Example: Node.js Application
export PORT=3000
node server.js
The application can read:
PORT=3000
from the environment.
This pattern is extremely common in backend development.
Environment Variables and Docker
Docker heavily relies on environment variables.
Example:
docker run -e PORT=3000 app
Containers commonly receive configuration through environment variables.
This allows:
- reusable containers
- flexible deployments
- safer configuration management
Environment Variables and Secrets
Environment variables often store:
- API keys
- database passwords
- tokens
Example:
export API_KEY="secret-key"
This is common but requires caution.
Environment variables are not automatically secure.
The .env Pattern
Many applications use:
.env
files.
Example:
PORT=3000
DB_HOST=localhost
API_KEY=secret
Applications load these variables automatically.
Very common in:
- Node.js
- Python
- Docker Compose
Why .env Files Matter
Benefits:
- centralized configuration
- easier deployments
- separation of config from code
This follows an important DevOps principle:
Configuration should be externalized.
Environment Variables in Linux Processes
Every process inherits an environment from its parent process.
Example:
systemd
└── bash
└── python
Environment variables propagate downward through process trees.
This explains why exports affect child processes.
Security Considerations
Be careful with sensitive variables.
Avoid:
export PASSWORD=supersecret
in shared environments.
Environment variables may sometimes be visible through:
- process inspection
- logs
- debugging tools
Modern infrastructure often uses dedicated secret managers instead.
Common PATH Problems
One of the most common Linux issues:
command not found
Usually caused by:
- missing installation
- incorrect PATH
- wrong executable location
Example troubleshooting:
echo $PATH
which docker
Real-World Example
Imagine installing a custom CLI tool.
File:
/home/john/tools/mycli
Make executable:
chmod +x mycli
Add to PATH:
export PATH=$PATH:/home/john/tools
Now:
mycli
works globally.
This is extremely common for developer tooling.
Why Environment Variables Matter for DevOps
Modern infrastructure relies heavily on environment variables.
Examples:
| Technology | Uses Environment Variables? |
|---|---|
| Docker | Yes |
| Kubernetes | Yes |
| CI/CD pipelines | Yes |
| Cloud platforms | Yes |
| Node.js apps | Yes |
Environment-based configuration is one of the foundations of cloud-native systems.
Linux Philosophy and Environment Variables
Environment variables reflect core Linux philosophy:
- modularity
- flexibility
- process isolation
- composability
Applications receive configuration from the environment rather than hardcoded settings.
This makes systems:
- easier to automate
- easier to deploy
- easier to scale
The Bigger Picture
Once you understand environment variables and PATH, Linux starts feeling much more logical.
You begin understanding:
- how commands are discovered
- how applications receive configuration
- how shells manage environments
- how Docker containers are configured
- how CI/CD systems inject settings
Environment variables are one of the invisible foundations of modern infrastructure.
What Comes Next
In the next chapter, we will explore:
- shell scripting
- Bash fundamentals
- variables
- loops
- conditions
- automation
- writing reusable Linux scripts
This is where Linux truly starts becoming programmable.