[Solved] “bash: command not found” Error

To fix the ‘bash: command not found’ issue, it’s essential to confirm whether the command is installed and included in your PATH. Utilize the ‘which’ command for this purpose. If the command isn’t installed, you can rectify this by executing sudo apt install [command]. Alternatively, if the command resides within a script that’s not defined in the PATH, you can append it using export PATH=$PATH:/path/to/script.

What Causes ‘bash: command not found’ Error to Happen?

The “bash: command not found” error happens when the system cannot locate the command you’re attempting to run. It can occur for various reasons, such as the command not being installed, the command not being included in the system’s PATH variable, or typos in the command syntax.

The “bash: command not found” error typically occurs due to one of the following reasons:

  • Typos: If you mistype a command or enter an incorrect command name, Bash won’t be able to find it in the directories specified by the PATH variable.
  • Missing Installation: If you are unable to use a command due to a not found error, it could be that your system does not have the command installed.
  • Incorrect Path: Bash relies on this list to find executable files when a command is called without specifying the full file path. If the PATH variable is not configured correctly or lacks essential directories, Bash won’t be able to locate the corresponding executable file for the invoked command in the expected locations.

4 Ways to Fix the ‘bash: command not found’ Error

As said, the “bash: command not found” error in Bash happens when the system can’t find the file you specified in your path variable. Here are three different ways to fix the “bash: command not found” error in Bash:

1. Check Command Spelling and Syntax

The most common reason for encountering the “bash: command not found” error in Bash is due to incorrect spelling of the command. If you misspell a command or enter an incorrect command name, Bash won’t be able to recognise it and return the “bash: command not found” error to the terminal.

For instance, let’s check it by entering a misspelling of “ls” command:

Spelling and Syntax-lss comamndThe command does not return any output. It happened due to the typo or in other words, the /bin folder does not contain “lss command”.

So, to avoid “bash: command not found” error, make sure to enter the command without spelling errors:

 ls command in the terminalAs you can see, the ls command can now return the list of files and folders of the current directory without any error.

2. Install and Verify Command

Another potential reason for “bash: command not found” error is the command is not installed in your system.  Always be sure that the command you are trying to run is installed in your system.

Let’s say, you want to check the version of the VLC video player by using your terminal. But you found a “command not found” error upon calling the vlc --version command as shown below:

Verify vlc installationThis indicates that the VLC player is not installed in your system. So you need to install it before checking it’s version.

To install VLC player in your terminal, use the following command:

sudo apt install vlc

Installing vlc in the systemAfter entering the sudo password (if required), wait for a few seconds for the installation to be completed. Then write the vlc --version command to check the version of the installed VLC player in your terminal.

Checking version of vlcAfter executing the command, as you see the image shows the version of the VLC player with associated information.

3. Export the Command in PATH Variable

In Linux, a path is a variable that tells the system where to look for certain files or commands. Usually, the path variable contains paths like /usr/local/sbin, /usr/bin, /usr/local/bin, and more. Now, if the system cannot find the command in any directories listed in the PATH, it will report a “command not found” error. For instance, the ls command is resided in the /usr/bin folder. Now if it is not included in the PATH environment variable, you may encounter the “command not found” error.

See the below image to understand what will happen when the PATH variable does not contain /usr/bin folder:

Export the command in path variableFrom the image above, the “ls command” is unable to list the files and folders of the current directory since the  /usr/bin is not included in the PATH environment variable.

To avoid the “command not found” error, ensure the /bin folder, containing your command is in the PATH variable. For this, export the folder in the PATH variable using the following command:

export PATH="/bin:$PATH"

To test the effectiveness of the command, write the ls command in your terminal:

Test the exported command in path variable-1

As you can see, “ls command” can return the current files and folders list since it is now a part of the PATH variable.

4. Check If the Command is not an Alias

Understandably, Linux aliases are essential. They allow you to create abbreviated versions of longer commands for convenience. For instance, on Ubuntu, commands like ll (an alias for ls -l) and la (an alias for ls -a) are readily available.

However, imagine you’re accustomed to using ll and la on your system, but when you switch to another Linux machine, ll isn’t recognized. This occurs because it’s not a native command and cannot be installed separately. In such cases, verify if the command exists elsewhere by searching online. If it doesn’t, it’s likely an alias specific to certain systems.

To verify the defined aliases in your system, you can run the alias command in your terminal:

Check the aliasesAs you see, the ll and la commands are already in the system as alias forms. However, if they are not in the system, you have to set the alias with the following command.

alias alias_name='command'

Here, change the alias_name with your preferred name and replace the command string with the command you want to store.

set the aliasHere, an alias named year has been set which contains an accumulated command date | tail -c 5 in it. So, whenever the year alias is called in the terminal, it will return the current year.

5. Execute Bash File in the Current Directory

You might encounter “bash: command not found error” when you want to execute a bash script with an incomplete command line.
Let’s say you want to execute a bash file “Greetings” in your current directory. However, you did not add a dot slash ./ notation or bash command before the script name. In this situation, you will get an error as shown in the image below:

ommand not found while Executing Bash File in the Current DirectoryTo fix the bash: command not found error, use the “./” notation or bash command followed by the name of the Bash file while executing in the current directory:

Execute Bash File in the Current DirectoryAs you see now, the dot slash “./” notation and bash command both execute the Greetings file successfully.

How to Fix “sudo: [command name]: command not found” Error?

When you get the error “sudo: [Command Name]: command not found” in bash, it usually means that the command that you want to run with sudo privilege is either not installed on your system or is not in your system’s PATH.

To fix this error, make sure that the command you want to run exists on your system first. If it is, you can use sudo to specify the entire path to the command. Otherwise, you can either add the directory that contains the command to the PATH environment variable.

Let’s say, you found “sudo: vim: command not found” error for the Vim text editor. It implies that you either do not have Vim in your system or you need to add the file containing the executable Vim file to the PATH variable to access it globally.

sudo vim command not foundNow to fix this, install the Vim editor using the following command:

sudo apt install vim

installing vim command in linuxNow use vim --version command to verify the installation process:

checking vim command in the system fileAs you see, vim is already invoked in my system, which means that you don’t need to add the directory containing the command file to the PATH variable.

Note: If you still find “sudo: [Command Name]: command not found”  error, you may add the directory containing the command file to the PATH variable. This can be done by modifying your shell’s configuration file (e.g., ~/.bashrc or ~/.bash_profile) and adding a line like export PATH=$PATH:/path/to/directory. After saving the changes and sourcing the configuration file (e.g., with source ~/.bashrc), you should be able to use sudo with the command without encountering the error.

“bash: command not found” vs “sudo: command not found”

Below is a comparison between the “bash: command not found” and “sudo: command not found” errors:

Error Type Cause Context Example
“bash [command name] not found” Command not found in Bash shell’s search paths. Occurs when executing a command in Bash shell but it’s not installed or in the PATH. Typing an undefined command hello_world in Bash shell.
“sudo [command name] not found” sudo command found, but specified command after sudo not found. Occurs when executing a command with sudo, but the specified command after sudo is not found or not installed. Typing sudo apt-get update, but apt-get is not installed or in the PATH,   and sudo is available.

How to Fix If the “bash” Command is not Found in Linux?

If the bash command is not found in Linux, it indicates a critical issue as bash is the default shell for many Linux distributions. Here’s how to fix this issue:

  1. Verify Availability of Other Shells: First, ensure that other shells are available on your system, such as sh, dash, or zsh.
  2. Check System PATH: Verify that the directory containing the Bash executable is included in your system’s PATH environment variable. This allows the shell to locate the bash command when you try to run it. You can check your PATH variable by runnin
    echo $PATH

    If the directory containing the Bash executable (usually /bin) is not listed in the output, you’ll need to update your PATH variable to include it.

    To update the PATH variable, use the following command:

    export PATH="/bin:$PATH"
  3. Reinstall Bash (if Necessary): If Bash is installed but not found in the PATH, reinstalling it may resolve the issue. To install or reinstall the Bash package in Ubuntu, use the following command:
    sudo apt update
    sudo apt install --reinstall bash
  4. Boot into a Live Environment (if Necessary): If you’re unable to fix the issue while the system is running normally, you may need to boot into a live environment from a USB or CD/DVD. From there, you can access your system’s filesystem and diagnose and repair any issues preventing the bash command from being found.

By following these steps, you should be able to resolve the issue if the bash command is not found in Linux.

Conclusion

To sum up, the “bash: command not found” error can be very annoying for Linux users. But by understanding the reasons behind this error and coming up with effective solutions like installing missing packages, setting up paths, and checking command spellings, you can get rid of this problem and make sure that the command executes smoothly. This article aims to provide the solutions to the “bash: command not found” error briefly. If you have any further questions related to this issue, give a sought in the comment section. I am one click away from answering your query. Till then, Good Bye!

People Also Ask

What is a Bash error?

A Bash error is any error that occurs while using the Bash shell. The Bash shell is the default interpreter for the command-line on Unix-like systems such as Linux and MacOS. A Bash error can happen for a variety of reasons, including syntax errors in a command or script, permissions issues, improper use of a built-in command, or issues with environment variables.

Examples of Bash errors include:

  1. Syntax errors in commands or shell scripts, such as missing quotation marks or incorrect command options.
  2. Permission denied errors when attempting to execute a command or script without the necessary permissions.
  3. Command not found errors when trying to run a command that is not recognized or does not exist on the system.
  4. Variable unset or undefined errors when trying to access an undefined or unset variable in a script or command.

All in all, there are many reasons why you may encounter Bash errors and you may need to troubleshoot and debug to find and fix the underlying problems.

How to setup Bash commands?

Setting up Bash commands involves several steps, including writing scripts, ensuring they have proper permissions, and making them accessible via the PATH environment variable. Here’s a basic guide to setting up Bash commands:

  1. Write Bash Scripts: Create Bash scripts (.sh files) containing the commands you want to set up. You can use any text editor to write these scripts.
  2. Add Shebang Line: At the beginning of each Bash script, add a shebang line (#!) followed by the path to the Bash interpreter. This line tells the system which interpreter to use to execute the script. For example:
    #!/bin/bash
  3. Set Execution Permissions: Make the Bash scripts executable by setting the execute permission. You can do this using the chmod command. For example:
    chmod +x script.sh
  4. Choose Installation Directory: Decide on a directory where you want to store your Bash scripts. It’s common to use directories like /usr/local/bin or ~/bin.
  5. Move Scripts to Installation Directory: Move your Bash scripts to the chosen installation directory. For example:
    mv script.sh /usr/local/bin
  6. Update PATH: If you haven’t already, ensure that the installation directory is included in your PATH environment variable. This allows you to execute the Bash scripts from anywhere without specifying their full path.
  7. Verify Setup: Test your setup by running the Bash scripts from the command line. If everything is set up correctly, the scripts should execute without errors.

By following these steps, you can set up Bash commands/scripts on your system and make them easily accessible from the command line. This allows for automation and simplification of repetitive tasks.

How to install Bash command in Linux?

Bash is typically pre-installed on most Linux distributions as the default shell. However, if for some reason it’s not available, you can install it using your distribution’s package manager. Here’s how to install Bash on a few common Linux distributions:

  1. Debian/Ubuntu:
    sudo apt update
    sudo apt install bash
  2. Red Hat/CentOS:
    sudo yum install bash    # For older versions using yum
    sudo dnf install bash    # For newer versions using dnf
  3. Arch Linux:
    sudo pacman -S bash
  4. Fedora:
    sudo dnf install bash
  5. OpenSUSE:
    sudo zypper install bash

Once installed, you can set Bash as the default shell for your user by running:

chsh -s /bin/bash

After this, the next time you log in, Bash will be your default shell.

How to activate Bash in cmd?

To activate the Bash shell within the Windows Command Prompt (CMD), you need to use the Windows Subsystem for Linux (WSL). Here’s how you can activate Bash in CMD:

  1. Enable Windows Subsystem for Linux (WSL):
    • Open PowerShell as Administrator.
    • Run the following command:
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  2. Install a Linux Distribution from Microsoft Store:
    • Open the Microsoft Store app.
    • Search for your preferred Linux distribution (e.g., Ubuntu, Debian, etc.) and install it.
    • Launch the installed Linux distribution from the Start menu.
  3. Initialize Linux Distribution:
    • Upon first launch, you’ll be prompted to create a new user account and set a password for the Linux distribution.
  4. Open CMD and Run Bash:
    • Open CMD.
    • Type bash and press Enter.

After following these steps, you’ll be running the Bash shell within the Windows Command Prompt (CMD) via the Windows Subsystem for Linux (WSL). You can then execute Bash commands and scripts as you would on a Linux system.

Related Articles


<< Go Back to Bash Error Handling and Debugging | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Mohammad Shah Miran

Hey, I'm Mohammad Shah Miran, previously worked as a VBA and Excel Content Developer at SOFTEKO, and for now working as a Linux Content Developer Executive in LinuxSimply Project. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). As a part of my job, i communicate with Linux operating system, without letting the GUI to intervene and try to pass it to our audience.

Leave a Comment