Command Not Found Bash: Fix & Prevent It
In this article
If you see "command not found" in Bash, it means the shell could not locate an executable file by that name in any directory listed in your PATH environment variable. The fix is usually to install the missing program, correct a typo, or add the directory containing the command to your PATH. This guide walks through the exact checks and fixes, from the simplest to the more involved, so you can resolve the error and stop it from happening again.
What You'll Need
- Access to a terminal or command-line interface (local or remote via SSH).
- Basic familiarity with typing commands and reading output.
- Permission to install software (often sudo or administrator rights).
- The exact command name that triggered the error, including any typos.
Step-by-Step
- Check for a typo. Re-read the command you typed. Bash is case-sensitive:
LSis not the same asls. Look for extra spaces, missing hyphens, or a wrong letter. If you copied the command from a website, paste it into a plain text editor first to strip hidden formatting characters. - Confirm the command exists. Run
type -a commandname(replacecommandnamewith the actual word). If Bash reports "not found", the program is either not installed or not in yourPATH. If it prints a path, the command exists and the problem is likely a typo in how you called it. - Search your PATH manually. Run
echo $PATHto see the colon-separated list of directories Bash searches. Then usewhich commandnameorcommand -v commandnameto see if it resolves. If both return nothing, the executable is not in any of those directories. - Install the missing package. On Debian/Ubuntu-based systems, use
sudo apt updatethensudo apt install packagename. On Fedora/RHEL, usesudo dnf install packagename. On macOS with Homebrew, usebrew install packagename. The package name often differs from the command name (for example, thedigcommand comes from thednsutilspackage on Debian/Ubuntu), so search your package manager if unsure. - Add the directory to PATH temporarily. If the program is installed but in a non-standard location, run
export PATH="$PATH:/full/path/to/directory". This lasts only for the current terminal session. Test the command again immediately after. - Make the PATH change permanent. Open your shell startup file (usually
~/.bashrcfor interactive Bash sessions, or~/.bash_profile/~/.profileon macOS and some login setups) in a text editor. Add the sameexport PATH=...line at the end, save, then runsource ~/.bashrc(or the file you edited) to apply it without logging out. - Check file permissions and the shebang. If you are trying to run a script directly (e.g.,
./myscript.sh), ensure it is executable withchmod +x myscript.sh. Also confirm the first line is a valid shebang such as#!/bin/bash. A missing or wrong shebang can produce confusing errors. - Reload your shell environment. If you recently installed software or edited a startup file, close and reopen the terminal, or run
sourceon the relevant file. Some package managers update PATH only for new sessions.
Common Mistakes
- Assuming the package name equals the command name. Many tools ship inside differently named packages. Always verify with your package manager's search function.
- Editing the wrong startup file. Bash reads different files depending on whether it is a login shell or an interactive non-login shell. If your PATH change disappears after logout, you likely edited
~/.bashrcwhen you needed~/.bash_profile, or vice versa. - Forgetting to quote PATH. Writing
export PATH=$PATH:/new/dirusually works, but if your existing PATH contains spaces, quote it:export PATH="$PATH:/new/dir". - Typing a relative path without
./. To run a program in the current directory, you must use./program; Bash does not search the current directory by default for security reasons. - Using sudo when not needed. Installing system-wide packages requires sudo, but running ordinary commands as root is risky. Use sudo only for the install step.
Tips
- Use
command -vinstead ofwhichin scripts; it is more portable and reliable across shells. - Keep your PATH additions at the end of the file so they do not override system defaults unexpectedly.
- After installing a new tool, run
hash -rto clear Bash's remembered command locations if the shell still cannot find it. - Prefer installing software through your distribution's package manager or Homebrew rather than downloading random binaries; it keeps PATH and updates manageable.
- If you maintain custom scripts, place them in a dedicated directory (e.g.,
~/bin) and add that single directory to PATH once.
Troubleshooting
If the command still is not found after the steps above, work through these checks in order:
- Verify the install actually succeeded. Re-run the install command and read the output for errors. A failed download or missing dependency can leave the package uninstalled.
- Check for multiple Bash environments. If you use containers, remote servers, or a different user account, the software may be installed in one environment but not another. Confirm you are in the right shell and user.
- Look for architecture or version mismatches. A binary built for a different CPU architecture or OS version may not run. Use your package manager to get the correct build.
- Inspect aliases and functions. Run
aliasanddeclare -fto see if a name is shadowed by an alias or function that points to a missing program. Remove or correct the alias in your startup file. - Test with an absolute path. If
/usr/local/bin/toolnameworks buttoolnamedoes not, the issue is purely PATH configuration. - Restart the terminal or log out and back in. Some changes only take effect in a fresh session. If that fixes it, make sure your PATH export is in the correct startup file for your shell type.
If none of these resolve the error, consult the official documentation for the specific tool or your operating system's package manager. Avoid downloading unofficial binaries from untrusted sources.
Frequently Asked Questions
Why does Bash say "command not found" when the program is installed?
The program's directory is probably not in your PATH. Check with echo $PATH and add the correct directory using export PATH=... in your shell startup file.
What is the difference between "command not found" and "No such file or directory"?
"Command not found" means Bash could not locate an executable by that name in PATH. "No such file or directory" usually means the path you gave does not exist, or a script's interpreter (shebang) is missing.
How do I make a PATH change permanent?
Add the export PATH=... line to your shell's startup file, such as ~/.bashrc or ~/.bash_profile, then reload it with source or open a new terminal.
Can I run a command without adding it to PATH?
Yes. Use the full path to the executable, for example /opt/tool/bin/toolname, or change into its directory and run ./toolname.
Why does the command work with sudo but not without?
sudo uses a different, often more restrictive, PATH and environment. If a command works only with sudo, its directory is likely in root's PATH but not yours. Add it to your user PATH instead of relying on sudo.
Does "command not found" mean I need to reinstall Bash?
No. Bash itself is working; it simply cannot find the specific program you typed. Reinstalling Bash would not fix a missing third-party tool.