Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Shell and Other Basics  ›  Lesson

Environment Variables — Settings That Travel

Environment Variables 12 minA variable set with NAME=value stays in the shell until it is exported, after which child processes receive a copy.

Environment Variables — Settings That Travel

Programs often need to know things about you, such as your home folder, your username, your preferred editor and where to look for commands. They don't ask each time. They read environment variables, small named values that the shell passes to every program it starts.

The rule

Set it, export it, and only children see it.

Set a variable with NAME=value, with no spaces around the =. Read it with a $ in front:

$ GARDEN_DIR=/home/asha/garden
$ echo $GARDEN_DIR
/home/asha/garden

At this point it's only a shell variable, and the programs the shell starts can't see it. Export it to put it in the environment:

$ export GARDEN_DIR
$ export EDITOR=nano        # set and export in one step

Exported variables are copied into every child process the shell starts, such as a script, an editor or another shell. A child gets its own copy, so if a script changes a variable, the shell that started it keeps the old value.

A few you'll see everywhere:

Variable Holds
HOME your home directory; ~ is a shortcut for it
USER your username
PATH where the shell looks for commands
EDITOR the editor tools should open for you
PWD the current directory
SHELL your login shell

Useful tools:

  • env or printenv lists every exported variable. printenv HOME shows just one.
  • unset GARDEN_DIR removes a variable.
  • NAME=value command sets a variable for that one command only: EDITOR=vim crontab -e.

Variables you set by typing disappear when the terminal closes. To keep one, put the export line in your shell's startup file, such as ~/.bashrc for Bash, and it will be set in every new shell.

The shortcut

When a program "can't see" a setting, ask two questions: was it exported, and was it set in this shell or a parent of it? A variable only travels down the family tree, from a parent to its children, and only if it was exported.

Why doesn't Asha's script see GARDEN_DIR?Step 1 of 5
  1. The symptom

    Her script prints an empty line where the folder should be.

The other common trap is spaces: GARDEN_DIR = /home/asha/garden makes the shell try to run a command called GARDEN_DIR with two arguments.

Drills

Answer fast — trust the rule.

quick drill

Which line correctly sets a variable in Bash?

quick drill

Asha runs TOOL=shovel and then a script that prints $TOOL. The script prints nothing. Why?

quick drill

A script runs export EDITOR=vim. After it finishes, what is EDITOR in the terminal that started it?

quick drill

How do you run one command with LANG=C without changing LANG for the rest of the session?

Remember

  • Set it, export it, and only children see it: NAME=value, then export NAME.
  • If a program can't see a setting, check whether it was exported and whether it was set in this shell or a parent.
  • No spaces around =, and put export lines in ~/.bashrc to keep them.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe invisible variable

Asha set GARDEN_DIR=/home/asha/garden, but her script prints nothing for it. Explain why, fix it for this session, make it permanent, and explain why a script cannot change the variable back in her terminal.