Skip to main content

Environment Variable

An environment variable is a named key-value setting stored in a process's environment and inherited by its child processes. It is the standard way to pass configuration such as ports, regions, and runtime modes to an application.

Understanding Environment Variables

What Is an Environment Variable?

Every Linux process carries a set of key-value pairs called its environment. PORT, NODE_ENV, DATABASE_URL, and AWS_REGION are typical examples. Because the values live outside the code, the same Docker image can run as development, staging, or production simply by changing what is injected at startup.

How Environment Variables Are Inherited

When a process starts a child with fork and exec, the child receives a copy of the parent's environment. Changes the child makes do not flow back to the parent.

◈ DIAGRAM
Parent shell Child process
NODE_ENV=production -- exec --> NODE_ENV=production (copy)
PORT=4000 PORT=4000 (copy)
MY_VAR=hello (not exported) MY_VAR is NOT visible

A plain shell variable stays local to the shell. export is what turns it into an environment variable that children inherit.

Practical Commands

Bash
printenv # all variables
printenv NODE_ENV # one variable
MY_VAR=hello
bash -c 'echo $MY_VAR' # empty: not exported
export MY_VAR=hello
bash -c 'echo $MY_VAR' # hello
NODE_ENV=staging node server.js # set for one command only
unset MY_VAR
# Load a .env file (safe with spaces and quotes)
set -a
source .env
set +a
# Defaults and required values
echo "${PORT:-4000}" # default if unset or empty
echo "${DB_HOST:?DB_HOST is required}" # abort if unset
# Inspect a running process (same user or root only)
tr '\0' '\n' < /proc/1234/environ | grep NODE_ENV

Troubleshooting Environment Variables

Symptom Command What to Check
App ignores variable printenv VAR Was it exported
Child cannot see it export VAR Assigned but not exported
Variable works in shell, not cron env inside the job Cron has a minimal environment
Security

A process's environment can be read from /proc/PID/environ by the same user and by root, and it often leaks into crash dumps, CI logs, and docker inspect output. For sensitive values such as private keys or database passwords, prefer a secrets manager such as Vault or AWS Secrets Manager, delivered as a mounted file with tight permissions.

Common Mistake

Loading .env with export $(cat .env | xargs). It breaks on values containing spaces or quotes. Use set -a; source .env; set +a instead.

Remember

export is what makes a shell variable inheritable. Without it the value exists only in the current shell.

Frequently Asked Questions

Why use environment variables instead of a config file?

They keep environment-specific values out of the built artifact and out of version control, and nearly every language and container runtime reads them. This supports the twelve-factor approach of one build, many environments.

What is the difference between a shell variable and an environment variable?

A shell variable exists only inside the current shell. Exporting it adds it to the environment, so every child process inherits a copy.

Are environment variables safe for secrets?

They are fine for low-risk settings like log level, but they can leak through process inspection, logs, and dumps. Use a secrets manager or a restricted file for credentials.

Found a mistake, an outdated step or a broken link?