Fix 'bash: command not found' on Linux (PATH reset fix)
When bash can't find a command, it's usually a broken PATH or missing package. Here's how to fix it fast.
When does this error pop up?
You type a basic command like ls, sudo, or vi, and bash says command not found. It happens right after you mess with your .bashrc, .profile, or .bash_profile. Or maybe you installed something from source and accidentally overwrote PATH instead of appending to it. I had a client last week who copied a script from a forum that had export PATH=/some/custom/dir instead of export PATH=$PATH:/some/custom/dir. Boom — everything broke.
What's actually going on?
The PATH environment variable tells bash where to look for executable files. It's a colon-separated list of directories like /usr/local/bin:/usr/bin:/bin. When you set PATH to a single directory, you lost access to all the standard system commands. Bash can't find ls because /bin isn't in the list anymore. Same for sudo in /usr/bin.
The fix is to restore PATH to a sensible default that includes the standard directories.
The fix — step by step
Most of these steps work on Ubuntu, Debian, CentOS, or any modern Linux. The exact default PATH varies a little by distro, but I'll give you the one that works everywhere.
- Open a new terminal or reconnect via SSH.
If you're stuck in a broken shell session, just open a new one. The defaultPATHis usually set in/etc/profileand/etc/environment, which still load. That alone might fix it. Try typingecho $PATH— if your path looks like/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin, you're good. If it's just one entry, keep reading. - If you can't even open a new terminal, use the absolute path.
Type/bin/lsor/usr/bin/sudoto run commands by their full path. That bypasses PATH entirely. Once you're in, you can fix the file that broke it.
/bin/cat ~/.bashrc
Look for a line likeexport PATH=/somethingthat doesn't include$PATH. - Fix the offending file.
Edit your.bashrc(or.profileor.bash_profile— whichever you changed):
/usr/bin/nano ~/.bashrc
Find the line that sets PATH without the$PATHvariable. Change it from:
export PATH=/custom/dir
to:
export PATH=/custom/dir:$PATH
Or just comment it out with a#at the start if you don't need it. - Source the file or restart the shell.
source ~/.bashrc
Or just close and reopen the terminal. Your PATH should be back to normal. - If you don't know which file broke it, check them all.
Run these one by one to find the culprit:
/bin/grep 'export PATH' ~/.bashrc ~/.profile ~/.bash_profile /etc/profile
Look for any line that sets PATH without$PATHin it.
What if it's still broken?
Sometimes the file itself got corrupted or deleted. Here's what to do next:
- Restore
.bashrcfrom the system skeleton. Most distros keep a default in/etc/skel/. Copy it over:
/bin/cp /etc/skel/.bashrc ~/.bashrc
Then source it. - Check
/etc/environment. Some systems set PATH there. It should look likePATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin". If it's wrong, fix it with sudo (using absolute path):
/usr/bin/sudo /usr/bin/nano /etc/environment - If sudo itself says command not found, you need to become root via another method. Use
su -(with the hyphen to load root's profile), or boot into recovery mode. Once you're root, runexport PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binto get everything working again.
Had a client once who edited /etc/environment from a script and left a trailing quote. The whole system couldn't find any commands. We booted from a live USB, mounted the drive, and fixed the file. Takes 10 minutes and saves a reinstall.
Bottom line: always append to PATH, never overwrite it. If you do break it, these steps will get you back up in five minutes flat.
Was this solution helpful?