# Environment variables

> Named settings a program reads from outside its code, like where its database lives.

- Level: Intermediate
- Topics: Software
- URL: https://tidbit.legios.com.ar/environment-variables/
- In Spanish: [Variables de entorno](https://tidbit.legios.com.ar/es/environment-variables/)

**Environment variables** are name-and-value pairs that the operating system hands to every program when it starts, such as `DATABASE_URL=postgres://…` or `LOG_LEVEL=debug`. The program reads them instead of having those values written into its code. They are an old Unix idea; one you have probably used without noticing is `PATH`, which tells the computer where to look for commands.

Their main job today is to let the **same code** run in different places. On a developer's laptop `DATABASE_URL` points to a small local database; in production it points to the real one. The code does not change, only the environment around it. This split between code and configuration is one of the principles of the well-known **Twelve-Factor App** guide for building cloud software.

They are also the usual way to give programs **secrets**, like an API key or a database password, without putting them in the repository. During development many projects keep these in a file named `.env` that a library loads at startup; that file must be listed in `.gitignore` so it never reaches Git. In the cloud, services like AWS Lambda, containers and CI/CD pipelines all let you set environment variables, and for sensitive values they can be filled from a secrets manager.

## An analogy

Like a recipe that says "bake at the temperature your oven needs" instead of a fixed number. The same recipe works in every kitchen; each kitchen supplies its own setting.

## Keep in mind

- Configuration lives outside the code, so one build runs anywhere.
- Each environment, like local, staging or production, sets its own values.
- The common way to pass secrets without committing them.
- Keep `.env` files out of Git.

## Go deeper

- [Config, from The Twelve-Factor App](https://12factor.net/config)
- [Environment variable on Wikipedia](https://en.wikipedia.org/wiki/Environment_variable)

## Related terms

- [Operating system](https://tidbit.legios.com.ar/operating-system/): The base software that runs a device and lets every app share its hardware.
- [Database](https://tidbit.legios.com.ar/database/): An organized store of data that programs can search and update safely.
- [Cloud computing](https://tidbit.legios.com.ar/cloud-computing/): Renting computers over the internet instead of buying your own.
- [API key](https://tidbit.legios.com.ar/api-key/): A secret string that identifies your app when it calls someone else's API.
- [Git](https://tidbit.legios.com.ar/git/): A time machine for code: every change saved, by whom and why.
- [AWS Lambda](https://tidbit.legios.com.ar/aws-lambda/): Run a piece of code only when something happens, with no server to manage.
- [Container](https://tidbit.legios.com.ar/container/): A sealed box with a program and everything it needs to run anywhere.
- [CI/CD](https://tidbit.legios.com.ar/ci-cd/): Automation that builds, tests and ships every code change, so releases become routine.
