Back to Blog
Lesson 27 of the Linux: Linux Command Line for Developers course
LinuxAugust 14, 20263 min read

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.

linuxbashshelldevopsenvironment-variables
Detailed view of computer code highlighting syntax in colors on a screen.

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:

Bash
printenv

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:

Bash
MY_APP_PORT=8080

To verify it exists, use echo with a dollar sign prefix:

Bash
echo $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:

  1. Without export:

    Bash
    SECRET_KEY=12345
    bash -c 'echo $SECRET_KEY'
    # Output: (nothing)
  2. With export:

    Bash
    export 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.

  1. Set the variable: export WEB_ROOT=/var/www/my-project
  2. Verify it: echo $WEB_ROOT
  3. 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 = 123 will result in a "command not found" error because the shell thinks MY_VAR is a command. Always use MY_VAR=123.
  • Case Sensitivity: By convention, environment variables are uppercase (e.g., PATH), but they are case-sensitive. my_var and MY_VAR are two different variables.
  • Persistence: Variables set via export only 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.

Similar Posts