command not found (bash and zsh)
In short: The shell finds the entered program neither in the PATH folders nor as a built-in command.
In more detail: Message: bash: foo: command not found or zsh: command not found: foo. The shell searches for programs in the folders of the environment variable PATH. If the command is not found there, this error appears (exit code 127).
In Depth
Causes
- Typo in the command (
gtiinstead ofgit). - The program is not installed.
- It is installed, but its folder is not in the PATH (for example
~/.local/bin,/usr/local/bin,~/.cargo/bin). - A script in the current folder is called without
./. - After installation the terminal was not restarted.
- A wrong shebang line (
#!/bin/bsh) or Windows line endings (\r) in the script.
Solutions
- Check spelling and complete with Tab.
- Install:
sudo apt install packagename(or your distribution’s package manager). The shell often already makes suggestions (command-not-foundpackage). - Check the path:
echo $PATH,which command,type command. - Add to PATH (in
~/.bashrcor~/.zshrc):export PATH="$HOME/.local/bin:$PATH", thensource ~/.bashrc. - Own script: call
./script.sh, make it executable withchmod +x script.sh. - For
\rline endings:dos2unix script.sh.
Note
Never put . (current directory) at the front of the PATH: an attacker could slip you a file with an ordinary command name. On Windows the similar message reads: “The term … is not recognized as the name of a cmdlet …” or “… is not recognized as an internal or external command …”.
See also: Exit code 126/127, Permission denied, bash