HomeTerminal & Command LineEnvironment Variables & PATH
beginner10 min read· Module 3, Lesson 5

🔐Environment Variables & PATH

Understand PATH, set variables, and configure your shell

Environment Variables & PATH

What Are Environment Variables?

Environment variables are settings stored in your computer's memory that programs can read to understand how they should behave. Think of them as sticky notes your operating system keeps around so every program knows important details — like who you are, where your home folder is, and which language you speak.

Every time you open a terminal, the system loads a set of these variables into your session. Programs you run inherit them automatically.


Viewing a Single Variable with `echo`

To see the value of any environment variable, use `echo` followed by a dollar sign and the variable name:

```bash echo $HOME ```

Output (example):

``` /Users/sarah ```

The dollar sign (`$`) tells the shell: "Don't treat this as literal text — look up the value stored under this name."

```bash echo $USER ```

Output:

``` sarah ```

Without the dollar sign, the shell prints the literal word:

```bash echo USER ```

Output:

``` USER ```


Common Built-in Variables

VariableMeaningExample Value
`$HOME`Your home directory`/Users/sarah`
`$USER`Your username`sarah`
`$SHELL`The shell program you are using`/bin/zsh`
`$PATH`List of directories the shell searches`/usr/local/bin:/usr/bin`
`$PWD`Current working directory`/Users/sarah/projects`
`$LANG`Language and encoding setting`en_US.UTF-8`
`$EDITOR`Default text editor`vim` or `nano`
`$TERM`Terminal type`xterm-256color`

Try them out:

```bash echo "Hello, $USER. Your shell is $SHELL." ```


Setting a Variable (Temporary)

You can create your own variable in the current session:

```bash GREETING="Hello World" echo $GREETING ```

Output:

``` Hello World ```

Important: This variable only exists in your current terminal session. If you open a new terminal window, it won't be there.

Also, a plain variable assignment is NOT automatically visible to child processes (programs you launch from this shell). For that you need `export`.


Exporting Variables

The `export` command makes a variable available to any program or script launched from the current shell:

```bash export API_URL="https://api.example.com" ```

Now if you run a script, that script can read `$API_URL`.

You can also set and export in two steps:

```bash DB_HOST="localhost" export DB_HOST ```

Or combine assignment and export in one line:

```bash export DB_HOST="localhost" DB_PORT="5432" ```


Listing All Environment Variables

The `env` command prints every environment variable in your session:

```bash env ```

The output can be long. Pipe it to `grep` to search:

```bash env | grep PATH ```

You can also use `printenv` to get a specific variable:

```bash printenv HOME ```

Note: `printenv` does NOT use the dollar sign.


Unsetting a Variable

To remove a variable from your session:

```bash unset GREETING echo $GREETING ```

Output will be blank — the variable no longer exists.


What Is PATH and Why It Matters

`PATH` is the most important environment variable you will encounter.

The Phone Book Analogy

Imagine you want to call a restaurant. You don't memorize every phone number in the city. Instead, you look it up in a phone book. The phone book is organized so you can find what you need quickly.

`PATH` works the same way for your shell. When you type a command like `python` or `git`, the shell doesn't magically know where the program lives on your disk. It checks a list of directories — that list is `$PATH`.

```bash echo $PATH ```

Output (example):

``` /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin ```

Each directory is separated by a colon (`:`). The shell searches them left to right. The first match wins.

What Happens When You Type a Command

  1. You type `git`.
  2. Shell checks `/usr/local/bin/git` — does it exist? No.
  3. Shell checks `/usr/bin/git` — does it exist? Yes!
  4. Shell runs `/usr/bin/git`.

If the program is not found in any PATH directory, you get:

``` command not found: git ```


Finding Where a Command Lives

`which` Command

```bash which python ```

Output:

``` /usr/bin/python ```

`where` Command (zsh)

In zsh, `where` shows ALL locations of a command:

```bash where python ```

Output:

``` /usr/local/bin/python /usr/bin/python ```

`type` Command

```bash type ls ```

This tells you whether something is a built-in command, an alias, or an external program.


Adding to PATH

If you install a program in a custom location (say `/opt/mytools/bin`), the shell won't find it unless that directory is in PATH.

Temporary (current session only)

```bash export PATH="/opt/mytools/bin:$PATH" ```

This prepends your directory so it is searched first.

You can also append:

```bash export PATH="$PATH:/opt/mytools/bin" ```

Prepend vs. append matters when two directories contain a program with the same name — the one found first wins.


Shell Profile Files — Making Changes Permanent

Temporary variables disappear when you close the terminal. To make them persistent, you add them to a shell profile file.

Which File to Edit?

ShellLogin Shell FileInteractive File
bash`~/.bash_profile``~/.bashrc`
zsh`~/.zprofile``~/.zshrc`

Quick rule:

  • On macOS (default shell: zsh): edit `~/.zshrc`.
  • On Linux (common shell: bash): edit `~/.bashrc`.
  • If you use bash on macOS: edit `~/.bash_profile` (macOS treats every terminal as a login shell in bash).

Adding a Persistent PATH Entry

Open the file in your editor:

```bash nano ~/.zshrc ```

Add this line at the bottom:

```bash export PATH="/opt/mytools/bin:$PATH" ```

Save and close the file.

Adding a Persistent Variable

```bash export EDITOR="code" export API_KEY="sk-abc123" ```

Add these lines to your profile file as well.


The `source` Command — Reloading Configuration

After editing a profile file, you have two choices:

  1. Close the terminal and open a new one.
  2. Use `source` to reload the file immediately:

```bash source ~/.zshrc ```

For bash:

```bash source ~/.bashrc ```

`source` reads the file and executes every line in your current session. A shorthand is the dot (`.`) command:

```bash . ~/.zshrc ```


Why API Keys Use Environment Variables

Hardcoding secrets in your source code is dangerous:

```python

BAD — anyone who sees your code sees your key

api_key = "sk-abc123secretkey" ```

Instead, store secrets as environment variables:

```bash export OPENAI_API_KEY="sk-abc123secretkey" ```

Then read them in your code:

```python api_key = os.environ["OPENAI_API_KEY"] ```

Benefits:

  • Security: The key is not in your code or Git history.
  • Flexibility: Different machines can have different keys.
  • Best practice: Every cloud platform (AWS, Heroku, Vercel) uses environment variables for configuration.

You can also use `.env` files with tools like `dotenv`, but never commit `.env` files to Git. Always add `.env` to your `.gitignore`.


Windows Equivalents

On Windows, the concepts are the same but the commands differ.

Command Prompt (cmd)

View a variable:

```cmd echo %USERNAME% echo %PATH% ```

Set a temporary variable:

```cmd set MY_VAR=hello echo %MY_VAR% ```

Set a permanent variable (survives reboots):

```cmd setx MY_VAR "hello" ```

Note: `setx` does NOT update the current session. You need to open a new Command Prompt to see the change.

PowerShell

```powershell $env:USERNAME $env:PATH $env:MY_VAR = "hello" ```

For permanent changes:

```powershell [System.Environment]::SetEnvironmentVariable("MY_VAR", "hello", "User") ```

GUI Method

  1. Press Win + R, type `sysdm.cpl`, press Enter.
  2. Go to Advanced tab.
  3. Click Environment Variables.
  4. Add or edit variables under User variables or System variables.

Quick Reference

TaskmacOS/LinuxWindows (cmd)
View variable`echo $VAR``echo %VAR%`
Set temporary variable`export VAR=value``set VAR=value`
Set permanent variableAdd to `/.zshrc` or `/.bashrc``setx VAR value`
List all variables`env``set`
Find command location`which command``where command`
Reload config`source ~/.zshrc`Open new terminal
Remove variable`unset VAR``set VAR=`

Summary

  • Environment variables are key-value settings your system and programs use.
  • Use `echo $VARIABLE` to view, `export` to set, and `unset` to remove.
  • `PATH` is a list of directories the shell searches when you type a command.
  • Use `which` or `where` to find where a command lives on disk.
  • Add permanent variables to your shell profile file (`/.zshrc` or `/.bashrc`).
  • Use `source` to reload your profile without restarting the terminal.
  • Store secrets in environment variables, never in source code.
  • On Windows, use `set` (temporary) and `setx` (permanent) or the System Properties GUI.