Skip to content

How to Fix Linux Environment Variable Not Set Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Linux Environment Variable Not Set Error. We cover key concepts, practical examples, and best practices.

Your application cannot find an environment variable and returns undefined or null — the variable was not exported, is set in the wrong shell config, or has a typo in the name.

The Problem

import os
print(os.environ.get("DATABASE_URL"))
# None

Even though you set it earlier:

DATABASE_URL=postgres://localhost:5432/db
echo $DATABASE_URL
# postgres://localhost:5432/db

Step-by-Step Fix

Step 1: Export the variable

# WRONG — only set in current shell, not exported to subprocesses
DATABASE_URL=postgres://localhost:5432/db

# RIGHT — export to subprocesses
export DATABASE_URL=postgres://localhost:5432/db

Step 2: Verify the variable is set

echo $DATABASE_URL

Or:

printenv DATABASE_URL

Step 3: Add to shell config for persistence

echo 'export DATABASE_URL=postgres://localhost:5432/db' >> ~/.bashrc
source ~/.bashrc

For zsh:

echo 'export DATABASE_URL=postgres://localhost:5432/db' >> ~/.zshrc
source ~/.zshrc

Step 4: Check for typos in the variable name

# WRONG
echo $DATABSE_URL

# RIGHT
echo $DATABASE_URL

Step 5: Set variables in the correct scope

For a single command:

DATABASE_URL=postgres://localhost:5432/db python app.py

For system-wide (in /etc/environment):

DATABASE_URL=postgres://localhost:5432/db

Note: /etc/environment does not use export and does not support variable expansion.

Step 6: Use default values in scripts

# Sets default if not set
DB_URL="${DATABASE_URL:-postgres://localhost:5432/default}"

Step 7: Check if the variable is used in the right shell

Variables set in .bashrc are not available in GUI applications. Use .profile or .pam_environment for display manager sessions.

Prevention Tips

  • Always use export when setting variables
  • Use printenv to list all environment variables
  • Add common variables to ~/.bashrc or ~/.profile
  • Use .env files with dotenv in development
  • Document required environment variables in README

Common Mistakes with environment variable

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world LINUX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between export and just setting a variable?

Setting a variable (VAR=value) makes it available only in the current shell. export VAR=value makes it available to child processes (sub-shells, programs, scripts). Without export, subprocesses cannot see the variable.

Why does my variable work in the terminal but not in a script?

Scripts run in a subshell and do not inherit shell-local variables. Only exported variables are inherited. In a script, you must source the config file or define the variable inside the script.

How do I set environment variables system-wide for all users?

Add them to /etc/environment (simple key=value, no export) or create a script in /etc/profile.d/ (with export). Systemd services can use Environment= in unit files or /etc/systemd/system.conf.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro