Docs / Linux Basics / Understanding Linux Environment Variables

Understanding Linux Environment Variables

By Admin · Feb 25, 2026 · Updated Apr 23, 2026 · 65 views · 1 min read

What Are Environment Variables?

Environment variables are key-value pairs that configure the behavior of processes and applications. They store settings like paths, credentials, and runtime configurations.

Viewing Variables

# Show all environment variables
env
printenv

# Show a specific variable
echo $PATH
echo $HOME
printenv USER

Setting Variables

# Set for current session only
export MY_VAR="hello"
export DATABASE_URL="mysql://user:pass@localhost/mydb"

# Set for a single command
DATABASE_URL="..." php artisan migrate

Persistent Variables

# For current user — add to ~/.bashrc or ~/.profile
echo 'export MY_VAR="hello"' >> ~/.bashrc
source ~/.bashrc

# System-wide — add to /etc/environment
MY_VAR="hello"

# For systemd services
# In the .service file:
Environment=MY_VAR=hello
EnvironmentFile=/etc/myapp/env

Common System Variables

VariablePurpose
PATHDirectories to search for executables
HOMECurrent user home directory
USERCurrent username
SHELLCurrent shell
LANGSystem locale
EDITORDefault text editor
TZTimezone

Application Configuration

Modern applications use environment variables for configuration instead of config files:

# .env file (loaded by frameworks)
DB_HOST=localhost
DB_USER=myapp
DB_PASS=secretpassword
REDIS_URL=redis://localhost:6379
API_KEY=sk_live_xxx

Security

  • Never commit .env files to version control
  • Use chmod 600 .env to restrict access
  • Environment variables in docker run -e are visible in docker inspect

Was this article helpful?