Skip to content
Codescope

Variables & the environment

Shell variables, exported variables, and what a child process inherits.

intro7 min5 steps

Before you start

Every process — every running program — has its own **environment**: a list of NAME=value pairs it can read. The shell sets these; programs it starts inherit them.

Some are famous: PATH lists the directories the shell searches for commands, HOME is your home directory, EDITOR is the program git commit should open. Most programs care about specific names and quietly ignore the rest.

This lesson shows the difference between a shell variable (only your current shell can see it) and an *exported* variable (child processes inherit a copy). Get this wrong and your scripts "can't find" values that seem obviously present.

Current shell

variables live in the shell process
greeting
hello
local only

Shell-local. No child process can see it.

Step 1 of 5. A shell variable is local. `greeting="hello"` creates a variable inside the current shell process. You can read it with `$greeting`, but it exists nowhere else. No spaces around `=` — `greeting = "hello"` would try to run a command called `greeting` with arguments `=` and `"hello"`.

01 / 05

Step 01 / 05

A shell variable is local

greeting="hello" creates a variable inside the current shell process. You can read it with $greeting, but it exists nowhere else.

No spaces around = — greeting = "hello" would try to run a command called greeting with arguments = and "hello".

session.shShell
# Shell variable — local to this shellgreeting="hello"echo $greeting          # hello# Export makes it visible to child processesexport PATH="/usr/bin:/bin"export EDITOR="vim"# Child process inherits exported variablesbash -c 'echo $EDITOR'   # vimbash -c 'echo $greeting' # (empty — not exported)# env shows all exported variablesenv | grep EDITOR# Unsettingunset greetingecho $greeting            # (empty)