Linux for Developers Course
Linux for Developers
/
Intermediate

Environment Variables

Definition

Dynamic-named values that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs.

Explain Like I'm New

Sticky notes stuck to your computer that programs can read. For example, a sticky note named `USER` says 'bob'. When a program needs to know who is using it, it reads the note.

Real World Example

A Node.js application needs to connect to a database. Instead of hardcoding the password into the code (a major security risk), the code looks for an environment variable named `DB_PASSWORD`. The server admin sets `export DB_PASSWORD='supersecret'`, and the app securely connects.

Common Use Cases

  • •Passing secrets to applications
  • •Configuring system paths

Interview Questions

basic

  • What extremely important environment variable tells Linux exactly which folders to search when you type a command like `ls` or `python`?

intermediate

  • What is the difference between setting a variable with `MY_VAR="hello"` versus `export MY_VAR="hello"`?

Flash Cards

Question

Which variable?

Click to reveal answer
Answer

The `$PATH` variable.

Question

With or without export?

Click to reveal answer
Answer

Without `export`, the variable is 'local' and only exists in your current terminal session. If you start a script or a child process, it won't see the variable. `export` promotes it to an Environment Variable, meaning any child process or script you run will inherit it and be able to read it.