IntroToLinux

2. Environment Variables

What Are Environment Variables?

Environment variables are dynamic values that affect how processes run on your system. They store information about your system environment.

Viewing Environment Variables:

# Show all environment variables
env

# Show all variables (including shell variables)
set

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

Important Environment Variables:

$HOME

echo $HOME
cd $HOME    # Same as: cd ~

$USER

echo $USER
whoami      # Same information

$PWD

echo $PWD
pwd         # Same information

$PATH

echo $PATH
# Example output: /usr/local/bin:/usr/bin:/bin:/usr/games

$PS1

echo $PS1
# Example: \u@\h:\w\$
# \u = username, \h = hostname, \w = working directory

$SHELL

echo $SHELL
# Example: /bin/bash

Creating Variables:

# Create a variable (no spaces around =)
MY_VARIABLE="Hello World"
NAME="Linux Student"
NUMBER=42

# Use the variable
echo $MY_VARIABLE
echo "My name is $NAME"
echo "The answer is $NUMBER"

Exporting Variables:

Variables created in shell are local by default. To make them available to child processes:

# Create and export in one step
export MY_GLOBAL_VAR="Available everywhere"

# Or create then export
MY_VAR="test"
export MY_VAR

# Check if variable is exported
env | grep MY_VAR

Why Are Environment Variables Important?

Environment variables are critical for security and flexibility in modern applications. They allow you to separate configuration from code, making applications portable and secure across different environments (development, testing, production).

🚫 The Wrong Way: Hardcoding Credentials

Imagine a backend application that needs to connect to a database:

graph TD
    A["<b>Backend Application</b><br/>(app.js - committed to GitHub)<br/><br/>const connectionString =<br/>'mongodb://admin:password123@<br/>db.company.com:27017/production'"]
    B["<b>Production Database πŸ—„οΈ</b><br/>(Credentials exposed in code!)"]
    
    A -->|"❌ Hardcoded Connection"| B
    
    style A fill:#ffcccc,stroke:#cc0000,stroke-width:3px,color:#000
    style B fill:#ffcccc,stroke:#cc0000,stroke-width:2px,color:#000

❌ PROBLEMS:

βœ… The Right Way: Using Environment Variables

Development Environment Flow

graph TD
    A["<b>Development Environment πŸ’»</b><br/><br/>export DB_CONNECTION_STRING=<br/>'mongodb://localhost:27017/dev'<br/>export DB_USER='dev_user'<br/>export DB_PASSWORD='dev_pass'"]
    B["<b>Backend Application</b><br/>(app.js - safe to share publicly)<br/><br/>const connectionString =<br/>process.env.DB_CONNECTION_STRING<br/>const dbUser = process.env.DB_USER<br/>const dbPass = process.env.DB_PASSWORD"]
    C["<b>Development Database πŸ—„οΈ</b><br/>(localhost:27017)"]
    
    A -->|"Environment Variables"| B
    B -->|"βœ… Secure Connection"| C
    
    style A fill:#d4edda,stroke:#28a745,stroke-width:3px,color:#000
    style B fill:#d4edda,stroke:#28a745,stroke-width:3px,color:#000
    style C fill:#d4edda,stroke:#28a745,stroke-width:2px,color:#000

Production Environment Flow

graph TD
    A["<b>Production Environment ☁️</b><br/><br/>export DB_CONNECTION_STRING=<br/>'mongodb://prod.db.aws:27017/prod'<br/>export DB_USER='prod_user'<br/>export DB_PASSWORD='$3cur3P@ss!'"]
    B["<b>Same Backend Application</b><br/>(Uses environment variables)<br/><br/>β†’ Automatically connects to<br/>the right database!"]
    C["<b>Production Database πŸ—„οΈ</b><br/>(Secure AWS Database)"]
    
    A -->|"Environment Variables"| B
    B -->|"βœ… Secure Connection"| C
    
    style A fill:#cce5ff,stroke:#007bff,stroke-width:3px,color:#000
    style B fill:#cce5ff,stroke:#007bff,stroke-width:3px,color:#000
    style C fill:#cce5ff,stroke:#007bff,stroke-width:2px,color:#000

βœ… BENEFITS:

Real-World Example

In a typical backend application:

# Set environment variables before running your app
export DB_HOST="localhost"
export DB_PORT="5432"
export DB_NAME="myapp"
export DB_USER="developer"
export DB_PASSWORD="dev_password"
export API_KEY="your_api_key_here"
export NODE_ENV="development"

# Now run your application
node app.js

Your application code can safely use these:

// No hardcoded secrets! βœ…
const dbConfig = {
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  database: process.env.DB_NAME,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD
};

🎯 Key Takeaways

Aspect Hardcoded Values Environment Variables
Security πŸ”΄ Exposed in code 🟒 Hidden from code
Flexibility πŸ”΄ Must edit code to change 🟒 Change without code edits
Portability πŸ”΄ Different code per environment 🟒 Same code everywhere
Team Sharing πŸ”΄ Everyone sees secrets 🟒 Each team member has own config
Version Control πŸ”΄ Secrets in Git history 🟒 Safe to commit code

πŸ’‘ Pro Tip: In production, environment variables are often stored in secure secret management systems like AWS Secrets Manager, Azure Key Vault, or HashiCorp Vault, adding an extra layer of security!


Next: β†’ Command History
Previous: ← Review Shell Basics Lesson 12 Recap
Lesson Home: ↑ Lesson 3: History & Variables Course Home: βŒ‚ Introduction to Linux