Computer scienceSystem administration and DevOpsCommand lineWorking with files

Searching executables

5 minutes read

While using bash commands, you need to understand that they are just executable files that are stored somewhere on your system. But how can we find out where they are located? How does bash get their location?

Simply put, their locations are passed through an environment variable $PATH. By accessing this variable, we can get command executables.

What is $PATH?

$PATH is an environment variable that contains directories of executable files. Through this variable, the shell and other programs can search for executables. To see the contents of $PATH, try the echo command. The results will look similar to this:

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

Here we can see that $PATH is a list of absolute paths to directories containing executables. These paths are separated by a colon.

Which command and how to use it

To locate executables within $PATH, you can use the which command. This command will show you paths to the executables you're looking for. The syntax of the which command is as follows:

which [options] executable_name..

For example, we could try to find the location of the echo command. In the terminal, it will look like this:

$ which echo
/bin/echo

As you can see, the which command gave us the path to the echo executable. If we write two or more file names, the output will contain all of their paths:

$ which echo ping
/bin/echo
/bin/ping

There could be more than one match for your request, though. The search for executables in $PATH goes from left to right, so the first match is the one that will be printed. To print all matches, you need to include the option -a in your which command:

$ which -a touch
/usr/bin/touch
/bin/touch

Sometimes these could be different commands using the same name or several different versions of the same command, but usually, one result is just a symlink to the other.

Modifying $PATH

Different UNIX and Linux shells, including Bash, allow modifying the $PATH variable. To do it, you can use any bash text editor. Here, we utilize vim. Through a text editor, you need to access the file named .bashrc. The command looks like this:

$ vim ~/.bashrc

After you type this command, the bashrc file will be opened. If the bashrc file wasn't created earlier, it will be created automatically. Here's how the first lines of the default file look in Ubuntu:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend
...

In different operating systems bashrc may look differently.

To modify $PATH, you need to go to the end of this file and enter the following line:

export PATH="$PATH:/<target_directory>"

In <target_directory> you can enter the path to any directory that contains executables you want to add to bash. In this case, we will add the ~/Desktop path. Afterward, you should save the changes and exit the file. Then you need to reload it by executing the source command:

$ source ~/.bashrc

After this, we can try to echo our $PATH once again. The results will look similar to the following:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:~/Desktop

So for example, after modifying $PATH, we can create a script named demo_script.sh on the desktop and make it executable with the chmod +x demo_script.sh command. Here's an example of its contents:

#!/bin/bash
echo "Script on the desktop"

So now, if you just write the command demo_script.sh in the terminal, the results will be the following:

$ demo_script.sh
Script on the desktop

Conclusion

$PATH and which are flexible and versatile tools intended to facilitate your work with executables. The possibility to modify $PATH gives us numerous ways to manage executables. On the other hand, the which command allows us to navigate between the executables included in $PATH.

33 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo