bash command not found

How to Fix Bash Command Not Found Error

‘Bash: command not found’ is a common error people get when they try to run a command in Linux. This happens because Linux shell is unable to find the command file for execution. In this article, we will learn how to fix this error. You can use the following steps in almost every Linux distribution.


How to Fix Bash Command Not Found Error

There are 3 common reasons why you get this error

  • Typo in command
  • Command is not installed
  • Command is not installed but its location is unknown


1. Typo in Command

This is the most common reason why people get this error. For example, you may have typed unmount instead of umount in order to unmount a disk partition. So check the following in your command:

  • Is the command name correct
  • Is there any unexpected comma or space in the command
  • Have you used proper case of characters, if needed
  • Have you left space between the command and its options


2. Ensure the command is installed

If the command is not installed on your system, then also Linux will give you this error. It doesn’t have the capability yet, to ask you if you want to install it on your system and then run the command. Instead, it may simply give you the command to install it on your system.

Here’s an example

$ docker ps

Command 'docker' not found, but can be installed with:

sudo snap install docker     # version 20.10.8, or
sudo apt  install docker.io

See 'snap info docker' for additional versions.

In such cases, you can simply copy the command present in the output to install the software package, and then run the command.

Sometimes you may get this error because the command you are trying is not only absent on your system but also has been deprecated. So it cannot be installed from the Linux repositories. In such cases, you may need to find an alternative, or install the software package from its source code. In some cases, you may just download the binary file of the command and place it in /usr/bin folder where Linux generally looks for the command’s files, when you run a command.

Sometimes, your user environment may not contain even the common commands like ls, pwd, etc. if your superuser has not given you access to those commands. In such cases, you need to contact your system administrator and ask him for access. This is especially true if you are working on FTP and your access has been restricted to specific folders, by the admin.


3. Check the command path

Sometimes you may get the error because you are incorrectly referencing the file path for the command. Here is an example. Let us say you have a shell script sample.sh in your present working directory. Now if you run the script the following way, just by calling its filename, you will get the error.

$ sample.sh
bash: sample.sh command not found

But if you run the script by specifying relative path of the file, it will run properly.

$ ./sample.sh
hello world

In some cases, the location of your command is not present in any of the PATH variables. PATH is a system variable that contains the list of folders where Linux will look to find every command that is run on your system. So when you run the command, Linux looks into the various folders listed in PATH variable and gives an error, since it is unable to find the binary file for your command.

In such cases, first, get the location of your command using which command.

$ which ls
/usr/bin/ls

Use the following command to get list of all folders added to PATH variable

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

In the above case, the folder containing ls command, /usr/bin is already present in PATH variable. So your ls command will work properly. Now let us say your command is installed in /home/ubuntu and you try to run it, then you will get ‘command not found’ error. In such cases, add the /home/ubuntu folder to the PATH variable, using the command below, highlighted in bold.

$ sample
bash: sample command not found

$ which sample
/home/ubuntu/sample

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

$ PATH=$PATH:/home/ubuntu

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/ubuntu

Please note, if you update PATH variable here, it will last only till end of session. If you want to permanently add folder to PATH variable, refer to our step by step guide here.

In this article, we have seen several ways to tackle the problem of ‘command not found’ in Linux. Almost everyone, beginner or advanced comes across this problem several times.

Also read:

How to Disable Strict Mode in MySQL
How to Fix Stdin: Not in GZIP Format
How to Find & Kill Zombie Process in Linux
How to Disable Strict Host Key Checking in SSH
How to Create Superuser in Django

Leave a Reply

Your email address will not be published. Required fields are marked *