Open In App

How to fix Bash: Command Not Found Error in Linux

Last Updated : 06 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The “command not found” error is a frequently encountered issue in Linux systems. This error occurs when the system is unable to locate the file specified in the path variable.

What is a path variable?

In Linux, the path variable informs the system about specific locations to search for files or commands. Typically, the path variable includes paths such as /usr/local/sbin, /usr/bin, /usr/local/bin, and more. Fortunately, we don’t need to specify the path variable each time, as it is pre-configured to search for programs in all directories.

Now that we have an understanding of the path variable, let’s explore how we can resolve the “command not found” error.

Different ways to fix this error:

Installing the package which is not present:

Sometimes when we write a command in Linux, the system is unable to find it because it is simply not present on our system. For example, we will try to run Python which is not installed in our system.

notfound-651ea81ff0a66

running python

You can see the command not found error, to resolve this simply install python. 

sudo apt-get install python

python-651ea81f55ddb

Installing python

Now when you type python, the error will be gone.

yess-651ea81f0d660

type pyhton

Explicitly telling bash where to look:

Sometimes, especially for scripts, we do not have to rely on the path we just execute them right away, so that they are executed wherever they are. We do this by putting “./” in front of them. We have created a script named GFG, let us execute it.

 ./GFG.sh

gfgsss-651ea81e6a190

./GFG.sh

But if we do not include the “./” before the script name then it gives us the command not found error. Let us see this from an example. We will create another script “Sample.sh” and try to execute it.

samplesa-651ea81e52308

Error

You can see that it is giving us the same error, we will resolve it in the next step.

Modifying the path variable:

Understanding Environment variables

Setting up the environment of a Linux shell requires certain variables, which exist within the child process of the shell. These variables are categorized as either local or global. In the context of writing an environment, it is customary to use capital letters preceded by a “$” symbol, such as the $PATH variable. These variables play a crucial role in modifying the behavior of applications on our system.

Let’s focus on the PATH environment variable, which allows us to specify the directory when searching for a command. By default, the value of this variable is stored in the /etc/profile file. Thanks to the configuration of the $PATH variable, we can avoid typing the entire path of a command each time and let the system know where to find it. To view all the available paths, use the following command:

echo $PATH

We are currently in the /home/mukul directory and it is not recommended to add the home directory to the path. So, we will create a bin folder (which will contain the Sample.sh script) and concatenate it to the path variable. Type the below commands to create a bin folder and move the Sample.sh into it.

mkdir bin
mv Sample.sh bin/Sample.sh

exprto-651ea81d8b81b

Creating a bin folder

Adding a directory to the PATH variable

By default, there are a lot of different directories which are present in this variable, we can also add our own directory into the PATH variable which will enable us to run our script or command from anywhere in the system. This definitely saves a lot of time and effort. There are two ways in which we can add to PATH:

  1. By appending (This adds our path at the end of the PATH variable)
  2. By prepending (This adds our path at the beginning of the PATH variable)

Now we have to attach the /home/mukul/bin to the path, use the below command:

export PATH = $PATH: /home/mukul/bin (appending)
export PATH =  /home/mukul/bin :$PATH (prepending)
result-651ea81d8b35b

You can see that our path has been added to the path variable.

Now we can run our “Sample.sh” script from anywhere and that too without any path or “./” mentions.

shhh-651ea81c7bb49

Running script

Note: Make sure to give permission to your file, or else it will give an error.

chmod +x Sample.sh

You have misspelled the command:

We all make mistakes and that is a natural thing, this error can also arise when we type the command incorrectly.

For example, If we type PQD instead of PWD we will get this error.

pwd-651ea81cbb874

Error

So, these were the four ways in which you can get rid of the Bash: Command Not Found Error in Linux.

Conclusion

This article explores the common issue of encountering the “command not found” error in Linux systems. This error often occurs when the system cannot find the specified file in the path variable. However, there are effective solutions to overcome this error, including installing missing packages, explicitly specifying paths, modifying the path variable, and ensuring accurate command spelling. By understanding and implementing these solutions, users can enhance command execution efficiency and enjoy a more seamless Linux experience.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads