解决 Linux 中的 “Bash: Command Not Found” 报错
命令
![](/data/attachment/album/202210/22/115959lu4u4x2xxw463uy4.png)



> 
> 本新手教程展示了在 Debian、Ubuntu 和其他的 Linux 发行版上如何解决 “Bash: command not found” 这一报错。
> 
> 
> 


当你在 Linux 中使用命令时，你希望得到终端输出的结果。但有时候，你会遇到终端显示“<ruby> 命令未找到 <rt>  command not found </rt></ruby>”这一报错。


![](/data/attachment/album/202210/22/120000w4apebmpb46p4wpb.png)


对于这个问题，并没有直截了当且单一的解决方案。你必须自己做一些故障排除来解决这个报错。


老实说，要解决它并不难。该报错信息已经给出了一些提示：“命令未找到”，这说明你的 shell（或者 Linux 系统）找不到你输入的那条命令。


shell（或 Linux 系统）找不到命令，有三个可能的原因：


* 你将命令的名称拼错了
* 该命令还没有安装
* 该命令是一个可执行脚本，但其位置未知


接下来，我们会详细介绍“命令未找到”这一报错的每一个原因。


### 解决“命令未找到”报错


#### 方法 1：再次检查命令名称有没有写错


每个人都会犯错误，尤其是在打字的时候。你输入的命令可能存在错别字（也就是你写错啦）。


你应该特别注意：


* 是否拼对了正确的命令名称
* 是否在命令与其选项之间加上了空格
* 是否在拼写中混淆了 1（数字 1）、I（大写的 i）和 l（小写的 L）
* 是否正确使用了大写字母或者小写字母


看看下面的示例，因为我写错了 `ls` 命令，所以会导致“command not found”报错。


![](/data/attachment/album/202210/22/120000i1ku6nulfr7ld5u5.png)


所以，请再次仔细确认你输入得对不对。


#### 方法 2：确保命令已安装在你的系统上


这是“命令未找到”错误的另一个常见原因。如果命令尚未安装，则无法运行该命令。


虽然在默认情况下，你的 Linux 发行版自带安装了大量命令，但是不会在系统中预装 *所有的* 命令行工具。如果你尝试运行的命令不是一个流行的常用命令，那么你需要先安装它。


你可以使用发行版的软件包管理器来安装命令。


![You may have to install the missing command](/data/attachment/album/202210/22/120001ymozzbgu42n94noo.png)


有时候，某一常用命令可能也不再能使用了，甚至你也不能够安装这个命令了。这种情况下，你需要找到一个替代的命令，来得到结果。


以现已弃用的 `ifconfig` 命令为例。网络上的旧教程依旧会让你使用 `ifconfig` 命令，来 [获取本机的 IP 地址](https://itsfoss.com/check-ip-address-ubuntu/) 和网络接口信息，但是，在较新的 Linux 版本中，你已经无法使用 `ifconfig` 了。`ifconfig` 命令已被 `ip` 命令所取代。


![Some popular commands get discontinued over the time](/data/attachment/album/202210/22/120000w4apebmpb46p4wpb.png)


有时候，你的系统可能甚至找不到一些非常常见的命令。当你在 Docker 容器中运行 Linux 发行版时，就通常如此。Docker 容器为了缩小操作系统镜像的大小，容器中通常不包含那些常见的 Linux 命令。


这就是为什么使用 Docker 的用户会碰到 [ping 命令未找到](https://linuxhandbook.com/ping-command-ubuntu/) 等报错的原因。


![Docker containers often have only a few commands installed](/data/attachment/album/202210/22/120002ha9q65e8w93nz0q3.png)


因此，这种情况下的解决方案是安装缺失的命令，或者是找到一个与缺失命令有同等功能的工具。


#### 方法 3：确保命令是真实的，而不是一个别名


我希望你知道 Linux 中的别名概念。你可以配置你自己的较短的命令来代替一个较长命令的输入。


一些发行版，如 Ubuntu，会自动提供 `ll`（`ls -l` 的别名）、`la`（`ls -a` 的别名）等命令。


![](/data/attachment/album/202210/22/120445dd3owrlatzvozvrv.png)


想象一下，你习惯于在你的个人系统上输入 `ll` 和 `la`，而你登录到另一个 Linux 系统，发现 `ll` 命令并不存在。你甚至不能安装 `ll` 命令，因为它不是一个真正的命令。


所以，如果你找不到一个命令，甚至不能安装，你应该尝试在互联网上搜索该命令是否存在。如果不存在，可能是其他系统上的一个别名。


#### 方法 4：检查命令是否是一个路径正确的可执行脚本


这是 Linux 新手在 [运行 shell 脚本](https://itsfoss.com/run-shell-script-linux/) 时常犯的错误。


即使你在同一目录下，仅用可执行脚本的名称，来运行可执行脚本，也会显示错误。



```
[email protected]:~/scripts# sample
-bash: sample: command not found

```

因为你需要显式指定 shell 解释器或可执行脚本的路径！


![](/data/attachment/album/202210/22/120002alymaxlvzoag9zhx.png)


如果你在其他目录下，在未提供文件正确路径的情况下，运行 shell 脚本，则会有“<ruby> 找不到文件 <rt>  no such file or directory </rt></ruby>”的报错。


![](/data/attachment/album/202210/22/120003t505wemt5nn3tt3a.png)



> 
> **把可执行文件的路径加到 PATH 变量中**
> 
> 
> 有时候，你下载了一个软件的压缩文件（tar 格式），解压这个 tar 文件，然后找到一个可执行文件和其他程序文件。你需要运行可执行文件，来运行那个软件。
> 
> 
> 但是，你需要在可执行文件的同一目录下或指定可执行文件的整个路径，才能运行那个可执行文件。这很令人烦扰。
> 
> 
> 你可以使用 `PATH` 变量来解决这个问题。`PATH` 变量包含了有各种 Linux 命令的二进制（可执行）文件的目录集合。当你运行一个命令时，你的 Linux 系统会检查 `PATH` 变量中的上述目录，以查找该命令的可执行文件。
> 
> 
> 你可以使用 `which` 命令，来检查某一命令的二进制文件的位置：
> 
> 
> ![](/data/attachment/album/202210/22/120004giprujgo1b51injj.png)
> 
> 
> 如果你想从系统上的任何地方都能运行可执行文件或脚本，你需要将可执行文件的位置添加到 `PATH` 变量中。
> 
> 
> ![](/data/attachment/album/202210/22/120004zzzk61fv0fqvfq1b.png)
> 
> 
> 然后，`PATH` 变量需要添加到 shell 的 rc 文件中，如此对 `PATH` 变量的更改就是永久性的。
> 
> 
> 这里的要点是：你的 Linux 系统必须了解可执行脚本的位置。要么在运行时给出可执行文件的整个路径，要么将其位置添加到 `PATH` 变量中。
> 
> 
> 


### 以上的内容有帮到你吗？


我懂得，当你是 Linux 新手时，很多事情可能会让你不知所措。但是，当你了解问题的根本原因时，你的知识会逐渐增加。


对于“未找到命令”报错来说，没有简单的解决方案。我提供给你了一些提示和要点，我希望这对你的故障排除有帮助。


如果你仍然有疑问或需要帮助，请在评论区告诉我吧。




---


via: <https://itsfoss.com/bash-command-not-found/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[chai001125](https://github.com/chai001125) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

When you use commands in Linux, you expect to see an output. But sometimes, you’ll encounter issues where the terminal shows ‘command not found’ error.

![bash command not found error](https://itsfoss.com/content/images/wordpress/2021/09/bash-command-not-found-error.png)

There is no straightforward, single solution to this error. You have to do a little bit of troubleshooting on your own.

It’s not too difficult, honestly. The error gives some hint already when it says “bash: command not found”. Your shell (or Linux system) cannot find the command you entered.

There could be three possible reasons why it cannot find the command:

- It’s a typo and the command name is misspelled
- The command is not even installed
- The command is basically an executable script and its location is not known

Let’s go in detail on each possible root cause.

## Fixing “bash: command not found” error

![bash command not found error 1](https://itsfoss.com/content/images/wordpress/2021/09/bash-command-not-found-error-1.png)

### Method 1: Double check the command name (no, seriously)

It is human to make mistakes, specially while typing. It is possible that the command you entered has a typo (spelling mistake).

You should specially pay attention to:

- The correct command name
- The spaces between the command and its options
- The use of 1 (numeral one), I (capital i) and l (lowercase L)
- Use of uppercase and lowercase characters

Take a look at the example below, where I have misspelled the common ls command.

![command not found error](https://itsfoss.com/content/images/wordpress/2021/09/command-not-found-error.png)

So, make double sure what you are typing.

### Method 2: Ensure that the command is installed on your system

This is another common reason behind the command not found error. You cannot run a command if it is not installed already.

While your Linux distribution comes with a huge number of commands installed by default, it is not possible to pre-install all the command line tools in a system. If the command you are trying to run is not a popular, common command, you’ll have to install it first.

You can use your distribution’s package manager to install it.

![command not found debian](https://itsfoss.com/content/images/wordpress/2021/09/command-not-found-debian.png)

In some cases, popular commands may get discontinued and you may not even install it anymore. You’ll have to find an alternative command to achieve the result.

Take the example of ifconfig command. This deprecated command was used for [getting Ip address](https://itsfoss.com/check-ip-address-ubuntu/) and other network interface information. Older tutorials on the web still mention using this command but you cannot use it anymore in newer Linux versions. It has been replaced by the ip tool.

![ifconfig command not found](https://itsfoss.com/content/images/wordpress/2021/09/ifconfig-command-not-found.png)

Occasionally, your system won’t find even the extremely common commands. This is often the case when you are running a Linux distribution in Docker containers. To cut down on the size of the operating system image, the containers often do not include even the most common Linux commands.

This is why Docker user stumble across things like [ping command not found error ](https://linuxhandbook.com/ping-command-ubuntu/?ref=itsfoss.com)etc.

![ping command not found ubuntu](https://itsfoss.com/content/images/wordpress/2021/09/ping-command-not-found-ubuntu.png)

So, the solution is to either install the missing command or find a tool that could do the same thing you were trying to do with the missing command.

### Method 3: Make sure that the command is real, not an alias

I hope you are aware of the [alias concept in Linux](https://linuxhandbook.com/linux-alias-command/?ref=itsfoss.com). You can configure your own shorter commands to replace the typing of a longer command.

Some distributions like Ubuntu automatically provide commands like ll (which is aliased to ls -l), la (aliased to ls -a) and so on.

![alias in ubuntu](https://itsfoss.com/content/images/wordpress/2022/01/alias-in-ubuntu-800x365.png)

Imagine that you are used to of typing ll and la on your personal system and you log on to another Linux system and find that ll command does not exist. You cannot even install the ll command because it is not a real command.

So, if you cannot find a command and it cannot even be installed, you should try searching on the internet if that command even exists. If it does not, probably it was an alias on some other system.

### Method 4: Check if it is an executable script with correct path

This is a common mistake Linux rookies make while [running a shell script](https://itsfoss.com/run-shell-script-linux/).

Even if you are in the same directory and try to run an executable script just by its name, it will show an error.

```
root@debian-test:~/scripts# sample
-bash: sample: command not found
```


You need to either specify the shell interpreter explicitly or its absolute path.

![bash script command not found error](https://itsfoss.com/content/images/wordpress/2021/09/bash-script-command-not-found-error-800x331.png)

If you are in some other directory and try to execute the shell script without giving the correct path to the file, it will complain about not finding the file.

![script file not found error](https://itsfoss.com/content/images/wordpress/2021/09/script-file-not-found-error-800x259.png)

#### Adding it to the PATH

In some cases, you download the entire software in a tar file, extract it and find an executable file along with other program files. To run the program, you need to run the executable file.

But for that, you need to be in the same directory or specify the entire path to the executable file. This is tiresome.

Here, you can [use the PATH variable](https://itsfoss.com/add-directory-to-path-linux/). This variable has a collection of directories and these directories have the binary (executable) files of various Linux commands. When you run a command, your Linux system checks the mentioned directories in the PATH variable to look for the executable file of that command.

You can check the location of the binary of a command by using the `which`

command:

![path location](https://itsfoss.com/content/images/wordpress/2021/09/path-location-800x241.png)

If you want to run an executable file or script from anywhere on the system, you need to add the location of the file to this PATH variable.

![adding executable to path variable linux](https://itsfoss.com/content/images/wordpress/2021/09/adding-executable-to-PATH-variable-linux-800x313.png)

The PATH variable then needs to be added to the rc file of the shell so that the changes made to PATH variable are permanent.

You get the gist here. It is important that your Linux system has knowledge about the location of the executable script. Either you give the path while running it or you add its location to the PATH variable.

## Did it help you?

I understand that when you are new to Linux, things could be overwhelming. But when you understand the root cause of the problem, it gradually improved your knowledge.

Here, there is no straightforward solution possible for the ‘command not found error’. I gave you some hints and pointers and that should help you in troubleshooting.

Similarly, you may encounter '[unable to locate package](https://itsfoss.com/unable-to-locate-package-error-ubuntu/)' error in Ubuntu. Here's what you need to know about it.

[[Solved] “E: Unable to locate package” Error on UbuntuThis beginner tutorial shows how to go about fixing the E: Unable to locate package error on Ubuntu Linux.](https://itsfoss.com/unable-to-locate-package-error-ubuntu/)![](https://itsfoss.com/content/images/wordpress/2019/11/unable_to_locate_package_error_ubuntu.png)


![](https://itsfoss.com/content/images/wordpress/2019/11/unable_to_locate_package_error_ubuntu.png)

If you still have doubts or need help, please let me know in the comment section.