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 commandsHOME — current user home directoryUSER — current usernameSHELL — current shell programLANG — system localeEDITOR — 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)