Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

>_
Terminal & Command Line
0/10 completed
Beginner
What is the terminal?Navigating foldersWorking with files and foldersViewing files
Intermediate
Wildcards and finding filesPipes and redirectionText processing: grep, sort, cut and friends
Advanced
Environment variables and PATHPermissions and processes
Pro
Shell productivity: aliases, scripts and dotfiles
Tracks / Terminal & Command Line / Advanced

Environment variables and PATH

Environment variables are named values available to every program you run from the shell. They configure tools, hold settings like the current user and language, and pass secrets like API keys to applications without hard-coding them.

Viewing variables

Linux terminal (simulator) · editable
Variable Holds
HOME Your home folder
USER Your user name
PATH Where the shell looks for programs (see below)
SHELL Your shell program
PWD The current folder
LANG Language and encoding settings
EDITOR Your preferred text editor (used by git, crontab…)

$NAME (or ${NAME}) expands to the variable's value. Use env or printenv to list them all.

Setting variables

Linux terminal (simulator) · editable
  • NAME=value sets a shell variable, visible only to the current shell. There must be no spaces around =!
  • export NAME=value sets an environment variable, which is passed on to programs you start.
  • unset NAME removes it.

⚠️ NAME = value (with spaces) is an error: the shell thinks NAME is a command.

Quotes matter

Linux terminal (simulator) · editable
  • Double quotes "…" expand variables.
  • Single quotes '…' keep everything literal.
  • ${NAME} makes it clear where the name ends: ${HOME}_backup.

PATH: how the shell finds commands

When you type ls, how does the shell know which program to run? It searches each folder listed in PATH, in order:

Linux terminal (simulator) · editable

PATH is a colon-separated list: /usr/local/bin:/usr/bin:/bin. The first match wins.

  • which cmd shows which file will run.
  • type cmd also tells you whether it's a shell builtin (like cd) or an alias.

"command not found"

Linux terminal (simulator) · editable

This error means no folder in PATH contains a program with that name. Typical causes:

  1. It isn't installed (or there's a typo).
  2. It's installed somewhere that isn't in PATH, like ~/.local/bin or ./node_modules/.bin.

Fix: add the folder to your PATH, usually in ~/.bashrc or ~/.zshrc:

bash
export PATH="$HOME/.local/bin:$PATH"

To run a program in the current folder, prefix it with ./ (./script.sh), because . is deliberately not in PATH, for security.

Making variables permanent

Variables you set disappear when you close the terminal. To keep them, add the export lines to your shell's startup file:

Shell File
bash ~/.bashrc (and ~/.bash_profile on macOS)
zsh (macOS default) ~/.zshrc
Linux terminal (simulator) · editable

After editing, reload it with source ~/.bashrc (or open a new terminal).

Environment variables in apps: .env files

Applications read their configuration from environment variables. This is the 12-factor app principle. In development, it's common to keep them in a .env file:

bash
# .env — NEVER commit this file to Git!
DATABASE_URL=postgres://user:secret@localhost:5432/app
STRIPE_API_KEY=sk_test_abc123
NODE_ENV=development
js
// Node.js reads them via process.env
const db = process.env.DATABASE_URL;
python
# Python reads them via os.environ
import os
db = os.environ["DATABASE_URL"]

Add .env to .gitignore, and commit a .env.example with placeholder values so teammates know which variables to set.

Practice

  1. Create a variable PROJECT set to website, then use it to cd into ~/projects/$PROJECT.
  2. Export a variable APP_ENV=production and print it with printenv.
  3. Find out where cat and sort live, and whether cd is a builtin.
  4. Run source ~/.bashrc, then use the ll alias it defines.
Linux terminal (simulator) · editable
✎ Check your understanding

1.What's wrong with NAME = Asha?

2.You installed a tool but get "command not found". What's the likely cause?

← Text processing: grep, sort, cut and friends