Situatie
When you’re trying to run a command (with or without sudo
) and get an error message that reads “Command not found“, this means the script or file you’re trying to execute doesn’t exist in the location specified by your PATH variable. What is this variable, and how can you run commands that it can’t find?
Solutie
Global variables come predefined in your login shell, but they aren’t immutable and can be modified or deleted according to your preferences. You can use the printenv
or env
commands to display the environment variables on your system:
$ 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
[...]
The env
command prints out all global environment variables. Variables are case sensitive, and all Linux distributions use uppercase for environment variable names by default.
A local variable exists only within a specific shell. Therefore, when you define a local variable, it’s only available in your current shell. It doesn’t propagate or persist to a new shell session unless you export it as a global variable.
Local variables are often defined in lowercase to avoid overwriting a global variable with the same name.
The PATH environment variable
The PATH global environment variable lists the directories your system searches for valid, executable commands. By default, it contains standard directories that normally store executables like /usr/bin
, /usr/local/bin
, and so on.
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}
[...]
Leave A Comment?