20

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?

3
  • 1
    This may not work on a BSD-ish system: try restoring a default path with PATH=$(getconf PATH), otherwise do PATH=/bin:/usr/bin Commented Jun 22, 2015 at 15:15
  • 1
    The "getconf PATH" suggestion does indeed work fine, on 10.10 at least.
    – calum_b
    Commented Jun 22, 2015 at 15:37
  • 3
    Since the PATH is mangled then glenn's suggestion will work with PATH=$(/usr/bin/getconf PATH)
    – fd0
    Commented Jun 22, 2015 at 16:24

6 Answers 6

28

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)

3
  • 5
    Users who can't fix the described problem themselves probably are better of using nano instead of emacs/vi to edit ~/.bash_profile.
    – nohillside
    Commented Sep 1, 2016 at 13:15
  • @nohillside: How does using nano help prevent such issues?
    – Lelouch
    Commented Jan 17, 2021 at 7:54
  • 2
    @lelouch it doesn’t. But nano is useable even for people not used to Terminal, emacs/vi ain’t.
    – nohillside
    Commented Jan 17, 2021 at 9:11
15

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
...
2
  • 1
    There's no need to export your minimal PATH. Any process started by the current shell will inherit the minimal PATH. Though, I would useexport PATH in a shell config file.
    – fd0
    Commented Sep 1, 2016 at 14:29
  • 1
    Right! I'm so used to type it this way in environnements where there are shell scripts at test.
    – dan
    Commented Sep 1, 2016 at 14:43
2

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

  1. in terminal of mac, enter:

    export PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    
  2. you can use your bash commands temporarily, enter:

    vim ~/.bash_profile
    
  3. restore PATH parameters by modifying

    export PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    
  4. save vim and exit, then enter:

    source ~/.bash_profile
    

OK, my computer works again!

2
  • It might be worth adding that one needs to be careful in step 3 do not destroy other content in the file.
    – nohillside
    Commented Mar 4, 2019 at 15:10
  • the .bash_profile was destroyed by my incorrect orders, there is nothing left except empty "export PATH = ", so this is the only way i had to re-back my mac Commented Mar 6, 2019 at 1:56
1

It because you overwrite the existing system PATH.

You can fix it by adding the "$PATH" in front of every line.

Example:

PATH=$PATH:/my/path/here
PATH=$PATH:/my/other/path/here
export PATH
-2
  1. Open your bash_profile by running /usr/bin/vim ~/.bash_profile. (vim ~/.bash_profile will no longer work because the path is not right).
  2. Get rid of the path you just added that messed everything up.
  3. Exit and restart the terminal so that source ~/.bash_profile runs automatically. (Once again it won't work as is, because the path is not correct).
0
-5

Open your .bash_profile, go to the line with your error command and put a # before it as first character of the line. Restart (to be sure).

1
  • How does he open his .bash_profile?
    – mmmmmm
    Commented Jun 23, 2015 at 15:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .