Linux Environment Variables
Environment variables are dynamic values that affect the behavior of processes and programs in Linux. They store system information, configuration settings, and user preferences that applications can access and use.
Overview
Environment variables serve several important purposes:
- System Configuration - Store paths, locale settings, and system preferences
- Program Behavior - Control how applications behave and where they find resources
- User Customization - Allow users to customize their shell and application environment
- Security - Store sensitive information like API keys and credentials
- Process Communication - Pass information between parent and child processes
Common Environment Variables
| Variable | Description | Example Value |
|---|---|---|
PATH |
Directories to search for executables | /usr/bin:/bin:/usr/sbin |
HOME |
User's home directory | /home/username |
USER |
Current username | john |
SHELL |
Default shell program | /bin/bash |
PWD |
Current working directory | /home/john/documents |
LANG |
System language and locale | en_US.UTF-8 |
EDITOR |
Default text editor | vim |
TERM |
Terminal type | xterm-256color |
Viewing Environment Variables
Display all environment variables
Shows all environment variables and their values
Alternative command to view variables
Another way to display all environment variables
View specific variable
Displays the value of a specific environment variable
View all variables (including shell variables)
Shows all variables, including shell variables and functions
Search for variables containing text
Finds all environment variables containing "java"
Setting Environment Variables
Set variable for current session
Creates an environment variable for the current shell session
Set variable without export (shell variable only)
Creates a shell variable that won't be inherited by child processes
Add to PATH variable
Appends a new directory to the existing PATH
Set variable for single command
Sets LANG variable only for the ls command execution
Set multiple variables
Sets multiple environment variables in one command
Permanent Environment Variables
User-specific variables (~/.bashrc)
Sets variables for the current user's bash sessions
User profile variables (~/.profile)
Sets variables for all shell types for the current user
System-wide variables (/etc/environment)
Sets variables for all users system-wide
System-wide shell variables (/etc/bash.bashrc)
Sets bash-specific variables for all users
source filename to reload the changes.
Unsetting Environment Variables
Remove environment variable
Removes the specified environment variable
Remove multiple variables
Removes multiple environment variables at once
Temporarily unset variable for command
Runs firefox with the DISPLAY variable temporarily removed
Special Shell Variables
| Variable | Description | Example |
|---|---|---|
$0 |
Script name or shell name | bash |
$1, $2, ... |
Command line arguments | first_arg |
$# |
Number of arguments | 3 |
$@ |
All arguments as separate words | arg1 arg2 arg3 |
$* |
All arguments as single string | arg1 arg2 arg3 |
$$ |
Process ID of current shell | 1234 |
$? |
Exit status of last command | 0 |
$! |
Process ID of last background job | 5678 |
Variable Expansion
Basic variable expansion
Both forms access the HOME variable value
Default values
Various ways to handle unset or empty variables
String manipulation
Extract parts of variable values or perform substitutions
Best Practices
Naming Conventions
- Use UPPERCASE for environment variables
- Use lowercase for local shell variables
- Use descriptive names (JAVA_HOME vs JH)
- Avoid spaces and special characters in names
- Use underscores to separate words
Security Considerations
- Don't store sensitive data in environment variables in production
- Use proper file permissions for configuration files
- Be careful with variables in shared environments
- Consider using secret management tools for credentials
- Avoid logging environment variables that contain secrets
Common Use Cases
Development Environment Setup
Setting up development environments for different programming languages
Customizing Shell Behavior
Customizing shell behavior and appearance
Application Configuration
Configuring applications through environment variables
Troubleshooting
Variable not found
Check if variables are properly set
PATH issues
Debug PATH-related issues
Configuration not loading
Debug configuration file loading issues