Docs / Linux Basics / Environment Variables in Linux

Environment Variables in Linux

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

Viewing Variables

# Show all environment variables
env
printenv

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

Setting Variables

# Temporary (current session only)
export MY_VAR="hello"
export DATABASE_URL="mysql://user:pass@localhost/mydb"

# Permanent 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"

Important System Variables

  • PATH — directories searched for executable commands
  • HOME — current user home directory
  • USER — current username
  • SHELL — current shell program
  • LANG — system locale
  • EDITOR — default text editor

PATH Management

# Add directory to PATH
export PATH="$PATH:/opt/myapp/bin"

# Add to ~/.bashrc for persistence
echo 'export PATH="$PATH:/opt/myapp/bin"' >> ~/.bashrc

Using .env Files

# Load variables from .env file
set -a
source /var/www/myapp/.env
set +a

# Or in a script
export $(grep -v '^#' .env | xargs)

Was this article helpful?