Fix 'bash: command not found' on Linux — Root Causes
This error means bash can't locate the command you typed. Usually a PATH issue, typo, or missing package. I'll show you how to fix it fast.
Quick Answer
Run echo $PATH to check your PATH. If the command's directory (like /usr/local/bin) is missing, add it with export PATH=$PATH:/usr/local/bin. Then run hash -r and try again. If that fails, the package isn't installed — use apt install or yum install to grab it.
Why This Happens
I've seen this error a thousand times on forums and in my own terminal. It's frustrating because you know you typed the right command. Bash looks for executables in every directory listed in your PATH variable, in order. If your command isn't in any of those directories, or if you mistyped the name (trust me, I've hit sudu instead of sudo more times than I'll admit), bash throws up its hands.
Common triggers: you just installed a new tool but its binary landed in /usr/local/bin, which isn't in your default PATH. Or you're on a stripped-down Docker container or minimal server image that doesn't include common utilities like ifconfig or telnet. On CentOS 7, for instance, ifconfig isn't installed by default — you need net-tools. Ubuntu 22.04 LTS dropped ifconfig entirely, pushing you to ip. So knowing the exact trigger matters.
How to Fix It — Step by Step
- Check for typos. I know it sounds dumb, but I've wasted 10 minutes on a typo before. Retype the command slowly. Case matters on Linux —
Lsisn'tls. - Print your PATH. Run
echo $PATH. You'll see a colon-separated list. Common entries:/usr/local/sbin,/usr/local/bin,/usr/sbin,/usr/bin,/sbin,/bin. If the directory containing your command is missing, that's the problem. - Locate the command. Use
which commandnameortype commandname. If it returns nothing, the command isn't installed. If it returns a path like/opt/myapp/bin/commandname, that directory must be in your PATH. - Add the directory to PATH temporarily.
export PATH=$PATH:/opt/myapp/bin. This only lasts for the current session. Test the command again. - Make it permanent. Edit your shell profile —
~/.bashrcfor bash,~/.zshrcfor zsh. Add the sameexport PATHline at the end. Thensource ~/.bashrcto reload. - Rehash the command cache. Bash caches command locations. Run
hash -rto clear it. Without this, bash might still complain even after fixing PATH. - Install the missing package. If the command doesn't exist at all, install it. For Debian/Ubuntu:
sudo apt update && sudo apt install packagename. For RHEL/CentOS/Fedora:sudo yum install packagenameorsudo dnf install packagename. Need to know the package? Search withapt search commandnameoryum whatprovides */commandname.
Alternative Fixes If the Main One Fails
Sometimes PATH is correct, the command is installed, and it still fails. Here's what to try next:
- Use the full path. If you know where the binary lives, call it directly:
/usr/local/bin/commandname. If it works, the issue is definitely PATH. Add that directory. - Check file permissions. Run
ls -l /path/to/command. If it doesn't have execute permission (-rwxr-xr-x), add it withchmod +x /path/to/command. - Verify the shell is bash. Run
echo $SHELL. If it shows/bin/shor something else, you might be in a different shell. Switch to bash withbashorchsh -s /bin/bash. - Check for broken symlinks. Use
readlink -f /path/to/command. If it returns a path that doesn't exist, the symlink is dead. Reinstall the package. - Try a subshell. Sometimes environment variables get messed up. Launch a fresh subshell with
bash -land see if the command works there.
Prevention Tip
Before you install a new tool, read the docs to see where the binary lands. I always add /usr/local/bin and ~/bin to my PATH in ~/.bashrc right after I set up a new system. And I keep a list of common packages I need — net-tools, traceroute, nano, curl, git — and install them immediately on a fresh install. That way, the 'command not found' error is rare from day one.
Also, set up a bash alias for debugging: alias path='echo $PATH | tr ":" "\n"' — this prints each directory on a new line. Makes spotting missing paths trivial.
Was this solution helpful?