What is The PATH Environment Variable in bash?

The PATH environment variable in Bash (and other Unix-like shells) is a special variable that contains a list of directories where the shell looks for executable programs.

When you type a command in the shell, it searches for that command in the directories listed in the PATH variable.

Each directory in the PATH variable is separated by a colon (:). The directories are searched in the order they appear in the PATH. If a command is found in any of the directories, the shell executes that command. If the command is not found in any of the directories, you’ll get a “command not found” error.

You can view the current value of the PATH variable by typing echo $PATH in your Bash shell.

Here’s an example PATH variable:


/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

In this example, the directories /usr/local/bin, /usr/bin, /bin, /usr/sbin, and /sbin are included in the PATH. When you type a command, the shell will search for it in these directories, in the specified order.

You can modify the PATH variable to include additional directories by adding them to the variable using the export command. For example, if you want to add a directory called /my/custom/path to the PATH, you can use the following command:


export PATH="/my/custom/path:$PATH"

This command adds /my/custom/path at the beginning of the PATH variable, followed by a colon (:) and the existing value of PATH.

Modifying the PATH variable allows you to run commands from custom directories without specifying the full path to the executable each time.

Leave a Comment

Your email address will not be published. Required fields are marked *