$PATH

What is PATH?

PATH is an environment variable in Unix-like operating systems (including Linux and macOS) and Windows. It specifies a list of directories that the system searches for executable files when you type a command in the shell.

Structure of PATH

  • The PATH variable is a colon-separated list of directories.
  • Each directory in the list is searched in order until an executable file matching the command name is found.

Example of a typical PATH value on a Unix-like system:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/home/user/bin

How PATH is Used

When you enter a command like ls or python, the shell looks in each directory listed in PATH for an executable file with that name. If it finds one, it runs the command. If not, it returns an error indicating the command was not found.

Viewing and Modifying PATH

You can view the current PATH using the echo command:

echo $PATH

To add a directory to PATH in a bash shell, you can modify it like this:

export PATH=$PATH:/new/directory/path

Example Use Case

Suppose you have a custom script located in /home/user/scripts and you want to be able to run it from anywhere without typing the full path. You would add /home/user/scripts to your PATH:

export PATH=$PATH:/home/user/scripts

To make this change permanent, you can add the above line to your ~/.bashrc or ~/.bash_profile file.