MacOS – I broke the PATH by editing the ~/.bash_profile and Bash will not recognize most commands

bashfindermacospathterminal

in .bash_profile I accidentally set the path to:

PATH="~/Tools/apache-maven-3.3.3/bin"

and now the default path is gone. Nothing in Terminal works anymore:

Korays-MacBook-Pro:~ koraytugay$ ls
-bash: ls: command not found
Korays-MacBook-Pro:~ koraytugay$ mkdir test
-bash: mkdir: command not found

I can not see hidden files in Finder so I can not modify .bash_profile from Finder as well.

How can I fix this situation?

Best Answer

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)