Troubleshooting Bash Command Not Found Error in Linux

The “bash: command not found” error is common and frustrating when using the Linux command line. This error occurs when a command is not recognized by the system, either due to non-installation or unavailability in the system’s PATH. 

This guide will walk you through the common causes of this error and provide solutions to troubleshoot and fix the problem. 

Reason 1: Wrong Command Name or Typing Error

One of the most common problems new users of Linux face is remembering the exact name of the command they’re using. So one of the main reasons for this error could be that users have entered a command with the wrong spelling or in capital letters.

Solution: Use the Correct Command Name

Users must ensure the correct spelling and syntax for the relevant command. Also, Linux commands are case-sensitive, so you must ensure that you have followed the required pattern. 

For instance, if the “bashtop” command’s name is written correctly, the result would be: 

Reason 2: Command is not Installed on the System

Although most of the commands are pre-installed on Linux-based OS, in some cases, users can face “Command not found but can be installed with.”

Solution: Install the Command

To fix the above error, let’s install bashtop from one of the commands in the above output.

$ sudo apt install bashtop

As the above image highlights, the user password is needed to start the installation process because the keyword “sudo” is used. There’s a confirmation of space allocation that requires you to enter “Y” in the middle.

Now, when the bashtop command is re-executed, it will run.

$ bashtop

The above image shows the output of the bashtop command of Linux.

You can also verify if the command is already installed using which command in Linux.

$ which cut

When executed, the “which cut” command displayed the path of the cut command, indicating that the command exists on the system.

Reason 3: Executing a Script With Incorrect Location

The Linux terminal, by default, doesn’t know anything outside the current directory. Since there are multiple directories in Linux, using the cd command is recommended to navigate between the directories.

For example, a script file named “GuessGame.sh” is located inside the Documents folder; let’s try to execute it.

Solution: Locate the Bash Script Properly

Let’s navigate to the “Documents” (or the directory where the script is placed) directory using this cd command.

$ cd Documents

Highlighted in the above output, the terminal is now in the “Documents” directory, and all contents can be accessed.

Let’s execute the script again.

$ bash GuessGame.sh

The script is now executed, indicating that it was the problem with the file’s location.

Reason 4: The command is not in the System’s PATH Environment Variable

While executing a command in the Linux terminal, the system needs to know where to find the executable file associated with that command. It checks a list of directories stored in the PATH environment variable and separated by a colon (:). The system searches through each directory to locate the executable file with the same name as the command you entered.

Solution: Add the Path of the Executable 

Run this command to display the commands included in the PATH variable.

$ echo $PATH

Now, when you see the contents of the PATH variable, let’s add the path of the “ls command.”

$ export PATH=$PATH:/usr/bin/ls

If there isn’t any error, this will add the location where the ls command is to the system’s PATH variable.

To find the location where the command is located, use the which command (in this case, it is the ls command).

$ which ls

When executed, the above command shows where the ls command is located. This link explains almost everything related to the PATH variable of Linux.

Conclusion

In Linux, there are some errors whose fix isn’t satisfactory, and the same is true with “Bash: Command not found.” However, this article discusses the possible solutions to that, and hopefully, it’ll be fixed because if any of the above doesn’t work, you need to reinstall the OS.