Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / The Linux Command Line

    Environment Variables and PATH

    An environment variable is a named piece of text every process carries and passes on to the processes it starts

    Chapter 8 of 124 min read

    The Plain Picture#

    Sticky notes on the assistant's desk: "My name is Priya. The database password is in the drawer. Look for tools in these three cupboards, in this order." Every task the assistant starts gets a photocopy of those notes.

    The Real Names#

    • An environment variable is a named piece of text every process carries and passes on to the processes it starts (they get a copy — changes in a child never flow back to the parent).

    • PATH is the most important one: a colon-separated list of folders where the shell looks for programs.

    Terminal
    echo $HOME                      # /Users/priya
    echo $PATH                      # /opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:...
    which python3                   # which file would run for "python3"? → /opt/homebrew/bin/python3
    env                             # every environment variable
    export APP_ENV=production       # set one for this shell AND every program it starts
    APP_ENV=test python3 app.py     # set one only for this single command

    Under the Hood#

    How the shell finds python3: it checks each folder in PATH, left to right, and runs the first python3 it finds. So the order matters: if two Pythons are installed, the one in the earlier folder wins. "I installed the new version but the old one still runs" is almost always a PATH order problem — which tells you which one wins.

    Making settings permanent: variables set with export disappear when the shell closes. To keep them, add the line to your shell's startup file: ~/.zshrc for zsh (your Mac), ~/.bashrc for bash. Run source ~/.zshrc to reload it now.

    Configuration through environment variables. Apps read settings like the database URL from environment variables, so the same code runs in development, testing and production with different settings. Locally these often live in a .env file:

    Terminal
    # .env   (never commit this — 07_git.md)
    DATABASE_URL=postgresql://studybuddy:s3cret@localhost:5432/studybuddy
    OPENAI_API_KEY=sk-...
    APP_ENV=development

    You'll load these in FastAPI with Pydantic settings (Backend in Production (coming soon)), and in the cloud they come from a secret manager instead (Cloud Security and Cost (coming soon)).

    Gotchas

    • Secrets in environment variables can leak through crash reports, ps e output and debug pages. Treat them as sensitive. Never print them in logs.

    • .env files must be in .gitignore. Leaked API keys on GitHub are found by bots within minutes (Git, Security Fundamentals).

    Check yourself

    4 questions
    1

    What is PATH, and how does the shell use it?

    Show answer

    A list of folders searched left to right for the program you typed; the first match runs.

    2

    Two copies of node are installed. How do you find out which one runs?

    Show answer

    which node (and echo $PATH to see the order).

    3

    What's the difference between export X=1 and putting export X=1 in ~/.zshrc?

    Show answer

    The first lasts only for the current shell session; the second applies to every new shell.

    4

    Why do apps read configuration from environment variables?

    Show answer

    The same code can run in every environment with different settings, and secrets stay out of the code.