FUNDAMENTALS A Complete Guide for Beginners
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:
The 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:
As 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:
This 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
After 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.
After 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:
From 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:
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:
As 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.
Here, 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:
To 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:
As 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.
Now to fix this, install the Vim editor using the following command:
sudo apt install vim
Now use vim --version
command to verify the installation process:
As 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.
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:
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: 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: By following these steps, you should be able to resolve the issue if the bash command is not found in Linux. 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! 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: 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. 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: 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. 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: Once installed, you can set Bash as the default shell for your user by running: After this, the next time you log in, Bash will be your default shell. 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: 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
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?
echo $PATH
export PATH="/bin:$PATH"
sudo apt update
sudo apt install --reinstall bash
Conclusion
People Also Ask
What is a Bash error?
How to setup Bash commands?
#!/bin/bash
chmod +x script.sh
mv script.sh /usr/local/bin
How to install Bash command in Linux?
sudo apt update
sudo apt install bash
sudo yum install bash # For older versions using yum
sudo dnf install bash # For newer versions using dnf
sudo pacman -S bash
sudo dnf install bash
sudo zypper install bash
chsh -s /bin/bash
How to activate Bash in cmd?
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart