Environment Variables in Linux — Configuring the Linux Environment¶
Environment variables are dynamic values maintained by the operating system that influence how programs, shells, and scripts behave. They store information such as user details, executable paths, language settings, application configurations, and runtime parameters. Environment variables are fundamental to Linux administration, shell scripting, DevOps, Docker, Kubernetes, and cloud computing.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand environment variables
- Differentiate shell variables and environment variables
- View existing variables
- Create environment variables
- Export variables
- Remove variables
- Configure the PATH variable
- Apply environment variables in production
Prerequisites¶
Complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 – Text Processing
- Module 4 – File Management and Permissions
- Module 5 Lessons 1–4
Why Learn Environment Variables?¶
Imagine you install Java.
Instead of running:
every time,
you simply run:
How does Linux know where Java is installed?
The answer is:
Environment Variables, especially the PATH variable.
What is an Environment Variable?¶
An environment variable is a name-value pair stored by the shell.
Example:
Applications use these values to determine how they should run.
Shell Variables vs Environment Variables¶
| Shell Variable | Environment Variable |
|---|---|
| Available only in the current shell | Available to child processes |
| Not inherited | Inherited by child processes |
Created with VAR=value | Exported using export |
Viewing Environment Variables¶
Display all environment variables.
or
Example:
Display a Specific Variable¶
Syntax:
Examples:
Common Environment Variables¶
| Variable | Description |
|---|---|
USER | Current username |
HOME | User's home directory |
PATH | Executable search path |
PWD | Current directory |
OLDPWD | Previous directory |
SHELL | Current shell |
HOSTNAME | System hostname |
LANG | System language |
TERM | Terminal type |
LOGNAME | Login username |
Creating a Shell Variable¶
Verify:
Output:
This variable exists only in the current shell.
Exporting an Environment Variable¶
Verify:
Output:
Now child processes can access it.
Difference Between Shell and Environment Variables¶
Create a shell variable.
Open a new shell.
Check:
Output:
The variable is not available.
Now export it.
Open another shell.
Check again.
Output:
Removing Variables¶
Verify.
Output:
Understanding PATH¶
Display:
Example:
When you run:
Linux searches each directory in PATH until it finds the executable.
Adding a Directory to PATH¶
Temporarily:
Verify:
Now executables inside /opt/tools/bin can be run without specifying the full path.
Environment Variables in Scripts¶
Example:
Run:
Listing Shell Variables¶
This displays:
- Shell variables
- Environment variables
- Functions
Finding One Variable¶
Common Commands¶
Display all variables.
Display one variable.
Create variable.
Export variable.
Remove variable.
Display all shell variables.
Real Production Examples¶
Set Java Home.
Configure Python.
Update PATH.
Configure Kubernetes.
Configure AWS CLI.
Production Perspective¶
Environment variables are heavily used in:
- Shell scripting
- Docker containers
- Kubernetes Pods
- Jenkins pipelines
- GitHub Actions
- GitLab CI/CD
- Python applications
- Java applications
- Node.js applications
Most modern applications read configuration from environment variables rather than hardcoded values.
Hands-on Lab¶
Task 1¶
Display all environment variables.
Task 2¶
Display your username.
Task 3¶
Display your home directory.
Task 4¶
Display your PATH.
Task 5¶
Create a variable.
Display it.
Task 6¶
Export the variable.
Verify.
Task 7¶
Remove the variable.
Task 8¶
List all shell variables.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
env | Display environment variables | Troubleshooting |
printenv | Display environment variables | Scripts |
echo $VAR | Display variable value | Verification |
export | Create environment variable | Application configuration |
unset | Remove variable | Cleanup |
set | Display shell variables | Debugging |
Production Troubleshooting Scenario¶
Scenario
A Java application fails to start.
Error:
Investigation:
The variable is missing.
Solution:
Verify:
The application now starts successfully.
Best Practices¶
- Use uppercase names for environment variables.
- Keep variable names descriptive.
- Avoid storing sensitive information (such as passwords or API keys) directly in environment variables on shared systems unless required and properly protected.
- Use
exportonly for variables that child processes need. - Configure permanent variables in profile files rather than setting them manually every session.
Common Mistakes¶
❌ Forgetting to export variables.
✅ Shell variables are not inherited by child processes.
❌ Overwriting the PATH variable.
✅ Incorrect:
Correct:
❌ Hardcoding application paths in scripts instead of using environment variables.
✅ Prefer using environment variables rather than hardcoding application paths in scripts.
Interview Questions¶
Beginner¶
- What is an environment variable?
- Which command displays all environment variables?
- What is the purpose of the PATH variable?
- How do you display the value of an environment variable?
Intermediate¶
- Explain the difference between shell variables and environment variables.
- What does the
exportcommand do? - How do you add a directory to the PATH?
- How do you remove an environment variable?
Architect Level¶
- Why are environment variables widely used in cloud-native applications?
- How do Docker and Kubernetes use environment variables?
- What are the security considerations when storing configuration in environment variables?
Summary¶
In this lesson, you learned:
- Environment variables
- Shell variables
- Viewing variables
- Creating variables
- Exporting variables
- Removing variables
- PATH
- Production use cases
Environment variables are a core part of Linux and modern application deployment. They allow applications and scripts to be configured without changing source code, making them an essential tool for Linux administrators, DevOps engineers, and cloud architects.
Key Takeaways¶
- Environment variables store configuration values used by the shell and applications.
- Use
envorprintenvto display environment variables. - Use
exportto make variables available to child processes. - Use
unsetto remove variables. - The
PATHvariable determines where Linux searches for executable programs. - Environment variables are widely used in automation, containers, and cloud-native applications.
What's Next?¶
Linux Profiles — Configuring User Login Environments
You'll explore:
- Login vs non-login shells
/etc/profile~/.profile~/.bash_profile/etc/environment- Shell startup sequence
- Configuring persistent environment settings
Understanding profile files will help you make environment variables and shell settings permanent across login sessions.