1.2. $PATH#

last update: Feb 07, 2024

A path usually indicates a string to identify a location of a file/directory in a directory tree. On the other hand, PATH is one of the environmental variables and is a little different from the path explained above.

When you input a command in the shell, the shell has to find and run the command. PATH specifies the places the shell should search for a command you input.

To see the PATH, run echo $PATH:

echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

The result shows PATH holds some directories (colon is a separator): /usr/local/bin, /usr/bin, /bin, /usr/sbin, /sbin

Tip

You can see all environmental variables with printenv

When you input a command, the shell searches /usr/local/bin at first. If the shell finds a command, it runs the command. If not, then it searches /usr/bin. If it finds the command, it runs the command. If not, then it searches /bin. This flow repeats until it finds and runs the command. So only the command found at first is run. If the shell could not find the command in all the directories in PATH, the shell returns zsh: command not found: xxx.

Command which shows the directories where (for example) the ls is.

which ls
# /bin/ls

1.2.1. Add command search path#

Add the following code in ~/.zshrc or ~/.bashrc to add a directory to PATH.

export PATH=$PATH:<command search path>
(or export PATH=<command search path>:$PATH)

and save ths changes.

source ~/.zshrc
source ~/.bashrc
source ~/.bash_profile

1.2.2. Environmental variables#

% printenv
...
SHELL=/bin/zsh
HOME=/Users/asdf
USER=asdf
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PWD=/Users/asdf
...

make a new environmental variable#

export US=/usr/sbin
echo $US
# /usr/sbin

each command below shows the same results

ls /usr/sbin
ls $US

Remove a environmental variable#

unset US