Environment variables are dynamic values that affect how processes run on your system. They store information about your system environment.
# Show all environment variables
env
# Show all variables (including shell variables)
set
# Show specific variable
echo $VARIABLE_NAME
echo $HOME
echo $USER
echo $PATH
/home/usernameecho $HOME
cd $HOME # Same as: cd ~
echo $USER
whoami # Same information
echo $PWD
pwd # Same information
echo $PATH
# Example output: /usr/local/bin:/usr/bin:/bin:/usr/games
echo $PS1
# Example: \u@\h:\w\$
# \u = username, \h = hostname, \w = working directory
echo $SHELL
# Example: /bin/bash
# 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"
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
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).
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:
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
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:
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
};
| 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