I broke my PATH by editing my ~/.bash_profile and Bash will not recognize most commands
Solution 1:
The simplest way is to restore from a backup e.g from Time Machine
However if the last backup is long enough ago that there is other changes you want to keep the you can edit .bash_profile
In terminal
In a terminal you can use the full path to the editors ( /usr/bin/emacs /usr/bin/vi or /usr/bin/nano) to edit ~/.bash_profile
e.g.
/usr/bin/nano ~/.bash_profile
Or you could open it with the default text editor (by default TextEdit) via:
/usr/bin/open ~/.bash_profile
In GUI - from Dock or Finder
But also you can do this from TextEdit (or other GUI editors) by File->Open and go to your home directory. Depending on if you have set Finder to show all files or not show hidden ones (the default) you will either see .bash_profile in the list or you can hit ⌘ CMD+⇧ SHIFT+. which reveals hidden files in Open/Save dialogs. (the shortcut is from this answer)
Solution 2:
Bring back a minimal PATH
Enter the following command to reset a minimal PATH:
PATH=/bin:/usr/bin
Fix ~/.bash_profile
Edit your ~/.bash_profile
with your preffered text editor vi
, emacs
or TextEdit
.
Always prefer a relative PATH construct:
PATH=${PATH}:...my_personal_added_pathes...
export PATH
Test it
Before proceeding as too many beginners do by restarting your session,
to discover that they can't anymore start a shell. Test your ~/.bash_profile
: open another Terminal
window, and check that it is working fine:
. ~/.bash_profile
ls
uptime
date
...
Solution 3:
i came across this mistake which takes me a whole afternoon to overcome. ls, git, mkdir etc. bash commands became invalid and reported an error:"-bash:xx:can't find files or directory" because i modified the .bash_profile in installing zsh. enter the following command to restore your ~/.bash_profile
-
in terminal of mac, enter:
export PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
-
you can use your bash commands temporarily, enter:
vim ~/.bash_profile
-
restore PATH parameters by modifying
export PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
-
save vim and exit, then enter:
source ~/.bash_profile
OK, my computer works again!