Environment Variables

Understanding and managing environment variables for system configuration and process behavior control

Environment variables are dynamic values stored in the system that affect how processes and programs behave. They provide a way to pass configuration information to applications and control system behavior without modifying code.

Key Concepts

  • Environment Variable: A named value accessible to all processes
  • Shell Variable: Local to current shell session only
  • Export: Makes shell variables available to child processes
  • Scope: Variables can be global (system-wide) or local (user/session)
  • Inheritance: Child processes inherit parent’s environment

Command Syntax

export VARIABLE=value - Create/modify environment variable unset VARIABLE - Remove variable env - Display all environment variables printenv [VARIABLE] - Show specific or all variables

Common Options

env -i - Start with empty environment env -u VAR - Unset specific variable printenv VAR - Show single variable value set - Show all variables (shell + environment)

Practical Examples

Example 1: Create and export variable

1
2
3
4
MY_VAR="hello world"
export MY_VAR
echo $MY_VAR
# Output: hello world

Creates variable and makes it available to child processes

Example 2: View all environment variables

1
2
3
4
5
env
# Output: Shows all environment variables
PATH=/usr/bin:/bin:/usr/sbin
HOME=/home/username
USER=username

Displays complete environment

Example 3: Temporary variable for command

1
MY_VAR="test" python script.py

Sets variable only for that command execution

Example 4: Check specific variable

1
2
3
echo $PATH
printenv PATH
# Both show PATH variable value

Two ways to display variable content

Use Cases

  • Application Configuration: Database URLs, API keys
  • System Behavior: Default editors, pagers, browsers
  • Path Management: Executable and library locations
  • Locale Settings: Language, timezone, formatting
  • Development: Compiler flags, library paths

which - Find executable location using PATH whereis - Locate binary, source, manual files source - Execute script in current shell bash -c - Run command in new shell with environment

Common Environment Variables

  • PATH: Executable search directories
  • HOME: User’s home directory
  • USER: Current username
  • SHELL: Default shell program
  • EDITOR: Default text editor
  • LANG: System language/locale
  • PWD: Present working directory
  • OLDPWD: Previous directory

Configuration Files

  • /etc/environment - System-wide variables
  • /etc/profile - System-wide shell settings
  • ~/.bashrc - User’s bash configuration
  • ~/.profile - User’s shell-independent settings
  • ~/.bash_profile - User’s bash login settings

Tips & Troubleshooting

Making Variables Persistent

1
2
3
# Add to ~/.bashrc for user-level persistence
echo 'export MY_VAR="value"' >> ~/.bashrc
source ~/.bashrc

Common Issues

  • Variable not found: Check if exported with export
  • Changes don’t persist: Add to appropriate config file
  • PATH issues: Verify directory exists and has execute permissions
  • Quoting problems: Use quotes for values with spaces

Security Notes

  • Never store sensitive data in environment variables in production
  • Be cautious with PATH modifications - can be security risk
  • Use env -i to start with clean environment when needed

Debugging

1
2
3
4
5
# Check if variable is set
[ -z "$MY_VAR" ] && echo "Variable not set"

# Show variable with default value
echo ${MY_VAR:-"default value"}