Skip to main content
junior
beginner
Linux

Environment Variables

Question

What are environment variables and how do you use them in Linux? How do you make them persistent?

Answer

Environment variables are dynamic values that affect processes and programs. Set temporarily with export VAR=value, view with echo $VAR or printenv. For persistence: add to ~/.bashrc (user-specific) or /etc/environment (system-wide). Common vars include PATH, HOME, USER, and custom app configs.

Why This Matters

Environment variables are crucial for configuring applications without hardcoding values. They're the standard way to pass configuration to containers, manage secrets (though not ideal for sensitive data), and customize behavior across environments like dev, staging, and production.

Code Examples

Setting and viewing variables

bash

Making variables persistent

bash
Common Mistakes
  • Forgetting to export variables (they won't be available to child processes)
  • Putting spaces around the = sign (VAR = value won't work)
  • Storing secrets in plain text environment variables in production
Follow-up Questions
Interviewers often ask these as follow-up questions
  • What's the difference between setting a variable with and without export?
  • How do environment variables work with Docker containers?
  • Why shouldn't you store sensitive secrets in environment variables?
Tags
linux
environment
configuration
bash
fundamentals