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.
Parent shell Child processNODE_ENV=production -- exec --> NODE_ENV=production (copy)PORT=4000 PORT=4000 (copy)MY_VAR=hello (not exported) MY_VAR is NOT visibleA plain shell variable stays local to the shell. export is what turns it into an environment variable that children inherit.
Practical Commands
printenv # all variablesprintenv NODE_ENV # one variable MY_VAR=hellobash -c 'echo $MY_VAR' # empty: not exportedexport MY_VAR=hellobash -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 -asource .envset +a # Defaults and required valuesecho "${PORT:-4000}" # default if unset or emptyecho "${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_ENVTroubleshooting 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 |
SecurityA process's environment can be read from
/proc/PID/environby the same user and by root, and it often leaks into crash dumps, CI logs, anddocker inspectoutput. 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 MistakeLoading
.envwithexport $(cat .env | xargs). It breaks on values containing spaces or quotes. Useset -a; source .env; set +ainstead.
Remember
exportis 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.