Variables and PATH

What this is. The shell keeps a set of named values called environment variables, and programs it starts inherit them. That is how configuration travels without being passed as arguments.

echo $HOME prints your home directory.

echo $USER prints your username.

export NAME=value sets one.

env lists them all.

PATH is the important one. When you type git, the shell does not search your whole machine. It looks in each directory listed in PATH, in order, and runs the first match it finds.

That single mechanism explains an enormous amount:

command not found means it is not in any PATH directory.

The wrong version runs means an older copy sits earlier in PATH.

Why ./script.sh needs the dot slash is that the current directory is deliberately not in PATH, because if it were, an innocuously named file in a downloaded folder could hijack a common command.

✦ which is how you check

which node tells you exactly which file would run. When a version seems wrong, which is the first command to reach for, because it distinguishes between the program being missing and the program being found somewhere unexpected. Those two problems look identical and have completely different fixes.

SANKOFA SANDBOX
learner:~$

Try it → Run `echo $HOME`, then `echo $PATH`, then `which grep`. Then set your own with `export PROJECT=archive` and use it with `echo $PROJECT`. Finally run `env | grep PROJECT` to find it among the rest.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Processes, Jobs, and Stopping Things →
← Back