Every system administrator got this error at least one time in a shell : “bash : command not found“.
However, you were pretty sure that you wrote the command correctly, or that you installed the tool that you are actually trying to execute.
So why are you getting this error?
The “bash : command not found” error can happen for various reasons when running commands in a Bash terminal.
Today, we are taking a look at the different ways to solve the “command not found” error in Bash.
Bash & PATH concepts
Before starting out with the solution, it is important to have a few concepts about what the PATH environment variable is and how it is related to the commands you run.
PATH is an environment variable that lists the different directories that your bash terminal will visit in order to find utilities on your system.
To have a look at your PATH environment variable, simply use the “echo” command with the PATH variable.
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
As you can see, PATH is defined by a list of different system paths delimited by colons.
They are the different paths visited by my interpreter in order to run commands.
If I were to remove an entry from the PATH, or remove the PATH all together, you would not be able to run commands in the bash without specifying the entire path to the binary.
It is an important point to understand because not being able to run a command does not mean that your binary was deleted on the system.
Now that you understand how environment variables are related to your bash interpreter, let’s see how you can solve your error.
Verify that the file exists on the system
The first step to solve this error is to verify that the command you are looking for actually exist on the system.
There are really no points going further if you mispelled the command or if you didn’t install it at all.
Let’s say for example that you cannot run the “ls” command.
Verify that the binary actually exists by searching for the binary on the system.
$ /usr/bin/find / -name ls 2> /dev/null /bin/ls /usr/lib/klibc/bin/ls
With the find command, you are able to locate the binary along with the directory where it is stored.
It is quite important because we will need to add this path to our PATH environment variable later on.
- Docker Exec Command With Examples | How to Run a Docker Exec Command inside a Docker Container?
- Pwd Command in Linux (Current Working Directory)
- Writing Scripts on Linux using Bash
Verify your PATH environment variable
Most of the time, you will run into the “bash : command not found” after changing your PATH environment in order to add new entries.
First, verify that the path you searched for before is listed in your PATH environment variable.
$ echo $PATH /home/user/custom:/home/user
As you can see here, the “/bin” directory is not listed in my PATH environment variable.
By default, the PATH is defined in the “/etc/environment” file for all the users on the system.
If your PATH environment variable is different from the one defined in the environment file, it is because you have overriden the PATH.
Now that you have two choices : either you know where you exported the PATH variable or you don’t.
Fixing your profile scripts : bashrc, bash_profile
In most of the cases, you modified the .bashrc or the .bash_profile file in order to add your PATH override.
To search where you exported your PATH, run the following command
$ /usr/bin/grep -rn --color "export PATH" ~/. 2> /dev/null ./.bashrc:121:export PATH="/home/devconnected"
This command returns the file where the PATH was exported as well as the line number.
Edit this file and add the path from the first section to the export statement.
$ nano /home/user/.bashrc export PATH="/home/devconnected:/bin"
Save your file and exit the nano editor.
For the changes to be applied, you will have to source your current bash terminal.
This will ensure that the .bashrc file is executed again in the current shell terminal.
$ source .bashrc
Why can you execute source without having to specify the full path? Because “source” is a shell built-in command. Try executing “builtin source .bashrc” for example
Now, you can try to execute the command you failed to execute before.
$ ls file devconnected file2 directory1 swap file3
You fixed the “bash : command not found” error on Linux!
Reset the PATH environment variable properly
Even if you solve your issue, you will have to define your PATH environment variable properly if you don’t want to modify your bashrc file all the time.
First, have a look at the PATH variable defined in the “/etc/environment” file.
$ cat /etc/environment PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
In order to reset your PATH environment variable on your environment, export the PATH defined in the environment file.
Now, modify your .bashrc file but use the $PATH syntax in order to append your paths to the existing PATH variable.
$ sudo nano ~/.bashrc export PATH="$PATH:/home/devconnected"
Exit the file and source your bashrc file for the changes to be applied.
$ source ~/.bashrc $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/devconnected
You have successfully resetted your PATH environment variable, you should not get the “bash : command not found” error anymore.
Execute the command as sudo
In some cases, your PATH environment variable may be perfectly configured but you will have to execute the command as sudo.
You may get this error or just a simple “permission denied” error.
In any cases, first make sure that you have sudo rights with the sudo command.
$ sudo -l User user may run the following commands on ubuntu: (ALL : ALL) ALL
If this is the case, you should be able to execute your command as sudo.
$ sudo <command>
You have solved the “bash : command not found” error on your system.
Verify that the package is correctly installed
In some cases, you think that your command is installed but you didn’t install the command to begin with.
Let’s say for example that you are looking to run the “htop” command but you are not able to do it.
$ htop bash : Command 'htop' not found
To verify if the command is correctly installed, depending on your distribution, run the following commands.
$ dkpg -s htop [Ubuntu/Debian] dpkg-query: package 'htop' is not installed and no information is available Use dpkg --info (= dpkg-deb --info) to examine archive files, and dpkg --contents (= dpkg-deb --contents) to list their contents. $ rpm -qa | grep htop [CentOS/RHEL]
In any case, you will have to install the command if you want to run it properly.
$ sudo apt-get install htop [Ubuntu/Debian] $ sudo yum install htop [CentOS/RHEL]
Now you can try to run the command that was missing.
In this tutorial, you learnt how you can solve the famous “bash : command not found” error that many system administrators encounter every day.
If you solve your issue with a solution that is not described in the article, make sure to leave a comment in order to help other administrators.
If you are interested in Linux system administration, we have a complete section dedicated to it on the website, so make sure to have a look.