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.
You can see the command not found error, to resolve this simply install python.
sudo apt-get install python
Now when you type python, the error will be gone.
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
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.
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
How to fix Bash: Command Not Found Error in Linux
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.