Level 0 · Module 2

Environment variables

Learn how processes receive named configuration values from their environment.

Difficulty
beginner
Time
8 min read
Prerequisites
Memory
Reading progress0% read

Not completed

By the end, you can

  • Describe an environment variable as a named string value.
  • Explain how child processes inherit environment values.
  • Use environment variables without confusing them with secure storage.

Summary

Environment variables are named strings supplied to a process. Programs use them for configuration such as language, search paths, and operating modes.

Introduction

The same program may need different settings on two computers. An environment lets the OS or launching process provide settings without changing program code.

Explanation

Each process has its own environment mapping names to string values. A child process usually receives a copy of its parent’s current environment. Changing a child’s value does not travel backward and rewrite the parent’s environment. Names are often case-sensitive on Unix-like systems. Programs must parse strings into numbers or booleans when needed.

Real-world example

Choosing a language

A program reads LANG=en_GB to select language and formatting. Starting it with LANG=ja_JP can change its behavior without editing the executable.

Pass settings to a child

environment-flow.txttext
shell environment: MODE=development
      ↓ launch
app environment:   MODE=development
app changes MODE locally; shell value stays unchanged

Code example

environment.shbash
export PROJECT_MODE=development
echo "$PROJECT_MODE"
PROJECT_MODE=testing ./run-checks

Common mistakes

  • Assuming environment values have types other than strings automatically.
  • Expecting a child process to change its parent’s environment.
  • Committing real credentials in environment example files.

Quiz

Knowledge check

A shell launches an app with MODE=testing. What does the app normally receive?

Try it

Separate code from configuration

Name three settings a web application could read from its environment, and identify which should be handled as sensitive.

Key takeaways

  • Environments map names to string values for each process.
  • Child processes generally inherit a copy of the launching environment.
  • Environment variables separate configuration from code but do not guarantee secrecy.
View module overview