如何修复 Linux 中的“未找到命令”错误
当您尝试运行命令(带或不带sudo
)并收到“未找到命令”的错误消息时,这意味着您尝试执行的脚本或文件不存在于 PATH 变量指定的位置。这个变量是什么?如何运行它找不到的命令?
了解环境变量
在计算中,变量是可以更改的值的占位符。虽然您不会将变量视为变量,但您在日常讲话中每天都会使用它们。当您说“我的笔记本电脑”时,您使用“笔记本电脑”作为您随身携带的计算机的通用变量或占位符,无论它是联想、Mac 还是装在精美外壳中的 Raspberry Pi。
环境变量是包含有关登录会话的信息的特殊变量。许多此类变量在安装或创建用户时默认设置。它们被存储以供系统 shell、应用程序和脚本在执行命令时使用。
有全局变量(即系统定义变量)和局部变量(即用户定义变量)。
全局变量
全局变量在登录 shell 中是预定义的,但它们并不是不可变的,可以根据您的喜好进行修改或删除。您可以使用printenv
或env
命令显示系统上的环境变量:
$ env
SHELL=/bin/bash
SESSION_MANAGER=local/kiwi.homelinux.local:@/tmp/.ICE-unix/1906,unix/kiwi.homelinux.local:/tmp/.ICE-unix/19
06
WINDOWID=153092103
COLORTERM=truecolor
XDG_CONFIG_DIRS=/home/tux/.config/kdedefaults:/etc/xdg:/etc/kde/xdg
LESS=-XR
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session1
HISTCONTROL=:ignorespace:ignoredups:ignorespace:ignoredups
PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig:/usr/local/share/pkgconfig:/usr/lib64/pkgconfig:/usr/share/pkgconfig
[...]
该env
命令打印出所有全局环境变量。变量区分大小写,所有 Linux 发行版默认使用大写作为环境变量名。
局部变量
局部变量仅存在于特定 shell 中。因此,当您定义局部变量时,它仅在当前 shell 中可用。除非您将其导出为全局变量,否则它不会传播或持久化到新的 shell 会话。
局部变量通常用小写字母定义,以避免覆盖同名的全局变量。
PATH 环境变量
PATH全局环境变量列出了系统搜索有效可执行命令的目录。默认情况下,它包含通常存储可执行文件(如 、 等)的标准/usr/bin
目录/usr/local/bin
。
When you type in a command, such as grep
or vim
, your system searches through all directories listed in your PATH variable, in the order that they're listed, until it finds an executable file by the same name. Should it fail to find one, it issues the "Command not found" error.
$ printenv PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/tux/.local/bin:/home/tux/bin
$ env $PATH
env: /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/tux/.local/bin:/home/tux/bin
5 ways to fix "Command not found" errors
There are several ways to fix this problem. Here are five of them.
1. Include the path
Not everything you want to execute needs to be in your path. You can execute files directly by specifying the path to the file you want to run. By identifying the file's location, you circumvent the need for your system to search your path at all.
For example, suppose you have a script called hello
that you want to run. It's located in your home directory, and you have already marked it as executable with chmod +x
:
$ ~/hello
hello world
By telling your system the file's location, the PATH variable is never involved, and the file runs as expected.
2. Add a new path
Alternately, you can add a new directory to your PATH. Add your executable files to that directory, and then you can run them without manually providing a path:
$ cp ~/hello ~/.local/bin
$ export PATH=$PATH:$HOME/.local/bin
$ printenv PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/tux/.local/bin
You may want to add the new PATH environment variables to your login shell by including them in your .bashrc
file as new settings.
3. Copy a file to an existing path location
If you want to execute your binary file or script, copy it to any of the directory paths already listed in the PATH environment variable:
$ sudo cp ~/hello /usr/local/bin/
$ hello
hello world
4. Tell Bash where to look
Probably the simplest option, especially for one-off scripts or applications, is to tell Bash not to consider the PATH but rather to "look here." Do this by placing a dot and a slash in front of the command, script, or application name. For the hello
script, it looks like this:
$ sudo ./hello
hello world
No permanent changes are made to the system. This might be handy if you're writing a script and want to test it before copying or moving it to its normal storage location (presumably along the PATH).
5. Install a package
Sometimes when you try to use a command and Bash displays the "Command not found" error, it might be because the program is not installed on your system. Correct this by installing a software package containing the command. For example, if you don't have Nmap installed, then the nmap
command fails when you type it into a terminal:
$ nmap
nmap: command not found
$ sudo dnf install --assumeyes --quiet nmap
$ nmap
Nmap 7.92 ( https://nmap.org )
Usage: nmap [Scan Type(s)] [Options] {target specification}
[...]
Stick to the path
The PATH variable is a powerful tool you can use to customize how your system responds to commands, so take some time to get comfortable with it. It's frequently used when running commands to find the command executable.
In this tutorial, you learned five ways to fix a "Command not found" error in your terminal—three of which rely on the PATH variable. Now that you know what variables are and how command executables are found, you won't be so mystified when the "Command not found" error appears on your screen.