Q1What does echo $HOME show?
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.
| Variable | Meaning | Example here |
|---|---|---|
$HOME | Path to the home directory | /root |
$PATH | List of directories to find commands (: separated) | /sbin:/usr/sbin:/bin:/usr/bin |
$PWD | Current working directory | /root |
$USER | Logged-in username | root |
$SHELL | Path to the login shell | /bin/sh |
$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
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 it | Meaning | Example |
|---|---|---|
$VAR | Substitute the variable's value | echo $HOME |
${VAR} | Reference with an explicit boundary (recommended) | echo ${HOME}/bin |
VAR=value | Assign in the current shell only | NAME=demo |
export VAR=value | Assign and carry into child processes | export NAME=demo |
$(command) | Substitute the command's output | echo $(pwd) |
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 $(...)
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.
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.
Knowledge Check
Answer each question one by one.
Q2Which is correct about the role of PATH?
Q3What is shown when you run echo $(pwd)?