EMZETT.
Login

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 (gti instead of git).
  • 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

  1. Check spelling and complete with Tab.
  2. Install: sudo apt install packagename (or your distribution’s package manager). The shell often already makes suggestions (command-not-found package).
  3. Check the path: echo $PATH, which command, type command.
  4. Add to PATH (in ~/.bashrc or ~/.zshrc): export PATH="$HOME/.local/bin:$PATH", then source ~/.bashrc.
  5. Own script: call ./script.sh, make it executable with chmod +x script.sh.
  6. For \r line 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