Learn by reading through in order

Environment Variables — PATH and export

Learn to reference variables with echo $VAR and env, what HOME and PATH mean, setting variables with export, and command substitution $(...) — hands-on in a browser terminal.

Referencing Variables — echo $VAR and env

Environment variables are settings shared by the shell and programs.

You store a value under a name, and writing $name substitutes that value.

Reference one by prefixing $, like echo $HOME, and run env to show the list of currently set environment variables.

As representative variables, HOME is the path to your home directory (here /root), and PATH is a colon-separated list of places to find commands.

Typing just ls works because the ls executable is in one of the directories listed in PATH, and the shell finds it there.

Other examples: PWD is the current working directory, USER is the logged-in username (here root), and SHELL is the path to the login shell.

VariableMeaningExample here
$HOMEPath to the home directory/root
$PATHList of directories to find commands (: separated)/sbin:/usr/sbin:/bin:/usr/bin
$PWDCurrent working directory/root
$USERLogged-in usernameroot
$SHELLPath to the login shell/bin/sh
Referencing environment variables
echo $HOME/root (home path)echo $PATHlist of places to find commandsenvshow all environment variables
$name shows one value; env shows the list of all variables.
echo $HOME      # home path (/root)
echo $PATH      # the command search path list
env             # show all environment variables

① Run echo $HOME and confirm the path to the home directory is shown.

② Run echo $PATH and confirm the places to find commands are listed, colon-separated.

③ Run env to show the list of set environment variables. (Run it correctly and an explanation will appear.)

Linux console
0 / 3 completed
Loading Linux Terminal...

Setting Variables — export and $(...)

name=value puts a value into a variable, and adding export name carries that variable into programs you launch later.

You can also write it in one line as export name=value.

You can reference with $name, but wrapping it as ${name} makes the boundary of the variable name explicit, so it expands without ambiguity even when characters follow directly, like ${HOME}/bin.

When in doubt, writing it in the ${name} form is recommended.

$(command) is command substitution: it runs the command inside and replaces it with that output as a string in place.

For example, echo $(pwd) shows the output of pwd (the path to your current location) as a string.

Use it when you want to put a command's result into a variable's value.

How to write itMeaningExample
$VARSubstitute the variable's valueecho $HOME
${VAR}Reference with an explicit boundary (recommended)echo ${HOME}/bin
VAR=valueAssign in the current shell onlyNAME=demo
export VAR=valueAssign and carry into child processesexport NAME=demo
$(command)Substitute the command's outputecho $(pwd)
export and command substitution
export NAME=demopass NAME tochild processes tooecho $NAMEshows demoecho $(pwd)replaced by pwd's output
export carries the value forward, and $(...) embeds a command's output.
export GREETING=hello   # set a variable and carry it forward
echo $GREETING          # hello
echo "today is $(date)" # embed a command's output with $(...)

① Set an environment variable with export NAME=demo.

② Run echo $NAME and confirm the value you set, demo, is shown.

③ Run echo $(pwd) and confirm that the output of pwd inside $(...) is shown as a string.

Linux console
0 / 3 completed
Loading Linux Terminal...

How PATH Works — How a Command Is Found

PATH is an environment variable listing the directories to find commands, separated by colons :.

When you type a command by name alone, without a path, like ls or cat, the shell looks through the directories listed in PATH from left to right and runs the first executable it finds.

That's why ls alone works without writing the full path /bin/ls.

Conversely, a command that's in none of the directories in PATH results in not found.

To call your own script by name alone, you need to add its location to PATH, which we'll look at next.

How a command is found via PATH
type lssearch PATH from the leftstop at the first match/sbinno lsnext place/usr/sbinno lsnext place/binls is hererun /bin/ls/usr/binnever reachedsearch has ended
When PATH is /sbin:/usr/sbin:/bin:/usr/bin, ls is searched from the left /sbin in order, found in /bin, and the search stops there. It never goes as far as /usr/bin.

How to Add to PATH

When you want to run your own command by name alone, add its location to PATH.

Write it as export PATH=${PATH}:/new/dir, appending a colon and the new directory to the end of the current PATH.

You include ${PATH} so you add to the existing search places without erasing them.

① First show the current search path with echo ${PATH}.

② Run export PATH=${PATH}:/opt/bin to add one location to the end.

③ Run echo ${PATH} again and confirm /opt/bin was added at the end.

Linux console
0 / 3 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1What does echo $HOME show?

Q2Which is correct about the role of PATH?

Q3What is shown when you run echo $(pwd)?