Serverspace Black Friday
JT
October 25 2024
Updated October 25 2024

How to solve "Bash command not found"?

Linux Maintenance

To understand what the root of the problem is, we need to look at how the mechanism of interaction with the OS itself works. Let's analyse it briefly, but let's dwell on each of the elements.

It happens that the shell in which the user is working generates an error command not found. Let's see how to solve it in this article.

How to solve the problem and what does the shell have to do with it?

To understand what the root of the problem is, we need to see how the mechanism of interaction with the OS itself works. Let's analyse it briefly, but dwelling on each of the elements.

Schema
Screenshot №1 — Schema

First the user, being in the shell, calls shell or bash, which is the very tool for OS management. In it he writes for example the command echo ‘Hello’, after which the terminal sends it to the shell and it contacts the kernel for information. Who is echo and where does it sit? The kernel responds and issues a thread to read the file, where the output is as follows:

Path
Screenshot №2 — Path

The shell reads the PATH variable, which specifies that all commands should be found in the /bin, /urs/bin, etc. folders. Suppose that a command was installed in the working directory of one of the users, and since it is not specified in the environments, it will not be executed. Or maybe it just doesn't exist? Consequently, we can identify several problems among the way of handling and working:

  • Software path is not written in environment variables, so it cannot be found;
  • Insufficient rights and privileges to search for the software in folders;
  • Software is not installed in the system;
  • Software name misspelling.

Let's go through each of them and describe several ways of solving the problem. The simplest is solution #1. Perhaps the command you are calling is not installed or the installed package uses a different command to invoke. For example, you need the nano editor, let's install it:

sudo apt install nano

If you have installed for example the ntp service, but when you call it directly it does not respond - then you need to find the package description!

sudo apt show ntp
APT show
Screenshot №3 — APT show

Here you can see the main tags, meta information about the package, as well as a Homepage link to a page that will describe the basic commands for this software. Let's go through the list:

List of commands
Screenshot №4 — List of commands

And with their help we can work with the service that will provide the installed package. But what if these two methods are not suitable and we downloaded the software manually and now the executable file is in the home directory? There are two ways out: move to the directory itself and run from it:

cd path && ./file_for_execute

In this case we specify the path to execute the file, which is in the given directory! Or we can add the path to a variable, note that the area you want to apply the variable to depends on the file. For example, /etc/environment will set the PATH variable for all users, and if a specific one is needed, then in ~/.bashrc in his home directory. Let's go to the home directory and open the file:

sudo nano ~/.bashrc

Add the path to the PATH line to start with a colon, so the interpreter knows which directory is where:

export PATH=$PATH:/home/your_user
New Path
Screenshot №5 — New Path

This command will be run when the shell is opened by the user, as if it is the first time the user connects or opens a new process. Export is the variable to be entered, PATH is the name of the variable, $PATH will call the previous value, and :/home/your_user is the string that will be added to the shared path. After that, run another shell and see the changes! For example, before the gg.sh file would not run without explicitly specifying it, but now it can:

Result of work
Screenshot №6 — Result of work

If you don't have sufficient resources than you can perform actions on powerful cloud servers. Serverspace provides isolated VPS / VDS servers for common and virtualize usage.

Create Server
Screenshot №7 — Create Server

Sometimes there are not enough permissions to run, for example system utilities will not work unless you specify sudo directly. This is because the interpreter searches the PATH path for the utility, and if it is a system utility, the normal user does not have access to the folder. Therefore, you need to add the user to sudoers:

usermod -aG sudo your-username

The field your-username should be replaced with the username of the user and run the command again, after which the software should start. There should be no more problems related to not found command by bash interpreter! In this article we have considered almost all ways and variants of solving the problem!

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
33145 North Miami, FL 2520 Coral Way apt 2-135
+1 302 425-97-76
700 300
ITGLOBAL.COM CORP
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.