Environment Variables: A Developer’s Guide to Shell Configuration
Master environment variables to decouple configuration from your code. Learn how to set, export, and inspect variables to manage your Linux shell environment.

Previously in this course, we used Project Task: Automating Service Scripts with Cron to schedule maintenance tasks. While hardcoding paths in scripts works for simple tasks, professional applications require a more flexible approach: environment variables.
Environment variables are key-value pairs stored in the shell’s memory. They allow you to pass configuration data—like API keys, database URLs, or directory paths—to your applications without modifying the source code. This is a foundational practice in modern DevOps, similar to how you might use Managing Environment Variables: A Docker Configuration Guide to keep containerized apps portable.
Understanding the Shell Environment
Every process in Linux starts with an "environment"—a set of variables inherited from its parent process. When you open a terminal, your shell (usually Bash) creates a unique environment for your session.
To see all currently defined variables in your session, run the printenv command:
Bashprintenv
You will see a long list of variables like USER, HOME, and SHELL. These are "exported" variables, meaning they are available to any child process you launch from this terminal.
Setting Temporary Variables
You can define a local variable by assigning a value directly in your terminal:
BashMY_APP_PORT=8080
To verify it exists, use echo with a dollar sign prefix:
Bashecho $MY_APP_PORT
Crucial distinction: This variable is local to your current shell session. If you start a new script or open another terminal tab, it will not exist there. It is not yet "exported."
Exporting Variables for Child Processes
If you run a script or a sub-shell, it cannot see your local variables. To make a variable available to child processes, you must use the export command.
Let’s test this with a simple sub-shell:
-
Without export:
BashSECRET_KEY=12345 bash -c 'echo $SECRET_KEY' # Output: (nothing) -
With export:
Bashexport SECRET_KEY=12345 bash -c 'echo $SECRET_KEY' # Output: 12345
By using export, you tell the shell to pass this variable to every process it spawns. This is exactly how you would Injecting Environment Variables in Kubernetes Pods later in your career to configure applications in production.
Hands-on Exercise: Preparing Project Configs
In our running project, we need to define a base directory for our web server content. Let’s set this as an environment variable to use in future scripts.
- Set the variable:
export WEB_ROOT=/var/www/my-project - Verify it:
echo $WEB_ROOT - Try accessing it in a sub-shell:
bash -c 'echo "My project root is: $WEB_ROOT"'
If you see "My project root is: /var/www/my-project", you have successfully exported your configuration.
Common Pitfalls
- Spaces in Assignment: Never put spaces around the
=sign.MY_VAR = 123will result in a "command not found" error because the shell thinksMY_VARis a command. Always useMY_VAR=123. - Case Sensitivity: By convention, environment variables are uppercase (e.g.,
PATH), but they are case-sensitive.my_varandMY_VARare two different variables. - Persistence: Variables set via
exportonly last for the duration of the current terminal session. Once you close the window, they are gone. We will cover how to make these permanent in an upcoming lesson.
FAQ
Q: How do I remove a variable?
Use the unset command: unset MY_VAR.
Q: Can I see only one specific variable?
Yes, use printenv MY_VAR or echo $MY_VAR.
Q: What is the difference between set and printenv?
set shows all shell variables (including local functions and internal shell settings), while printenv shows only exported environment variables.
Recap
We’ve learned that environment variables are the standard way to inject configuration into your Linux environment. You can set them locally for the current shell, or use export to pass them down to any child processes. By keeping these configurations outside your scripts, you increase the portability and security of your server setup.
Up next: The PATH Variable — where we’ll learn how Linux finds your executable commands.
Work with me

VPS Server Setup, Deployment & Hardening
Get your app live on a fast, secure server — properly configured, hardened, and deployment-ready. No more wrestling with the command line.

Custom WordPress Plugin Development
Custom WordPress & WooCommerce plugins built to standard — by the developer behind a plugin with 5,000+ active installs and a SaaS with 10,000+ users.

