Learn

Environment variables

Terminal

$PATH, $HOME, export, and where the shell keeps its memory between commands.

Environment variables are the shell's notepad: it reads from them to find the paths of installed programs, your username, the home folder, and all the configurations that dev tools store there (API keys, ports, etc.).

Reading a variable

shell
echo HOMEechoHOME
echo HOMEechoUSER
echo $PATH

The $ before the name tells the shell to substitute the variable with its value. Without $, it is just literal text.

  • $HOME: your personal folder (/Users/vicente or /c/Users/bloki).
  • $USER: your username.
  • $PATH: the list of folders where the shell looks for executables, separated by :.

Setting and exporting a variable

shell
MY_VAR="hello"
echo MYVARexportMYPORT=4000echoMY_VAR
  export MY_PORT=4000
  echo MYV​ARexportMYP​ORT=4000echoMY_PORT

Without export, the variable only exists in the current shell and is not passed to subprocesses. With export, it is inherited by commands you launch from that shell.

Listing all variables

shell
env
printenv HOME

env lists all exported environment variables. printenv <VAR> prints the value of a single variable.

Persisting a variable across sessions

A variable set with export disappears when you close the terminal. To have it defined every time you open a new terminal, add it to your shell configuration file:

shell
For bash (~/.bashrc or ~/.bash_profile)echo 'export MY_PORT=4000' >> ~/.bashrc
source ~/.bashrcFor zsh (macOS default)echo 'export MY_PORT=4000' >> ~/.zshrc
source ~/.zshrc

source reloads the file without reopening the terminal.

PATH in detail

shell
echo $PATH
which node
which python

$PATH contains something like /usr/local/bin:/usr/bin:/bin. When you type node, the shell searches for an executable named node in each of those folders, in order. which tells you which folder the command was found in.

To add a folder to PATH:

shell
export PATH="HOME/.local/bin:HOME/.local/bin:HOME/.local/bin:PATH"

.env files per project

In development, projects often use a .env file at their root to store configuration variables (API keys, database URLs, etc.). These files are not sourced automatically by the shell: a library in your code (like dotenv for Node.js or python-dotenv) loads them.

Bash variables (GNU Bash manual)

Concepts-ponts

Check off steps to unlock what comes next

Back to course