In "Programs Are Files", the shell found /usr/bin/whoami when you typed whoami. Here is how.
PATH is an environment variable. Its value is a list of directories, with : between them:
learner@tidepool:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binThe search
When you type a command name with no / in it, the shell looks for a program with that name in each PATH directory, from left to right. It runs the first one that it finds:
which does the same search and writes the result.
If no directory has the name, you get command not found. The program can exist, in a directory that is not in PATH.
A name with a / is a path
When the command has a /, the shell does not search. It runs that file:
learner@tidepool:~$ shop-status
bash: shop-status: command not found
learner@tidepool:~$ ~/bin/shop-status
shop is openAdd a directory to PATH
Put the new directory before the old value, and keep the old value:
learner@tidepool:~$ export PATH="$HOME/bin:$PATH"
learner@tidepool:~$ shop-status
shop is open| Line | Result |
|---|---|
export PATH="$HOME/bin:$PATH" | ~/bin is searched first |
export PATH="$PATH:$HOME/bin" | ~/bin is searched last |
export PATH="$HOME/bin" | The other directories are gone. ls stops working |
Use $HOME, not ~, inside the double quotes. The shell does not change ~ inside quotes.
The change lasts until you close this shell. The chapter "Customize Your Shell" shows how to keep it.