24

I was trying to add something to $PATH and it went totally wrong. I now can't run any commands such as ls. I've looked at this answer and used the following lines:

PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
PATH=$PATH:~/bin

These lines fix the problem temorarily; however, when I restart terminal it seems to forget these changes.

How do I permanently reset my $PATH?

I'm running the most recent version of Mountain Lion.

Nosrettap
  • 481
  • 2
  • 4
  • 7

5 Answers5

23

The suggested answer of removing .bash_profile is not a good idea in general. There are other things that can be set in that file besides PATH definitions.

If you want to undo the effects of your experimentation, just remove or comment out that PATH line with a #.

You don't want to edit the PATH from scratch, but append to it, as you did with your second line. The preferred method of adding something to your path would be:

export PATH=$PATH:$HOME/bin

EDIT Since your PATH is messed up, you don't have access to the usual commands to make these changes. As a temporary fix, you can define a new minimal path in a Terminal window (not in your .bash_profile) by typing:

PATH=/bin:/usr/bin

This will temporarily give you access to nano ls mv vi cat and rm -- the basic tools to check and edit your .bash_profile and fix your problem...

Repeat, do not put this PATH definition anywhere except for the duration of the session while you make your fixes.

beroe
  • 3,331
  • 5
  • 26
  • 41
  • For newer OS, your settings will be in .zshrc instead. Check which shell using echo $SHELL – beroe Dec 02 '20 at 21:45
  • There should be no need to explicitly export the variable; this particular variable will already be exported by your shell, so there is no need to do it again. – tripleee Feb 19 '22 at 09:48
15

I recommend:

source /etc/profile

This is what Mac uses to set the initial path, and it will put everything back in place excepting the items that you are adding for your user.

I do a decent amount of path modification in my ~/.bash_profile, and I've placed this at the top of the file because I was having issues with reloading my profile while I'm working after I tweak an alias in there or something, and it was adding duplicate references to my path. Instead of checking to see if it's already added, I just reset my path to the scratch version and re-append the items I want.

UPDATE: When switching to zsh, I changed my .bash_profile to .zshrc and then changed my reload profile alias to source the /etc/zprofile, which resets the path to scratch as described earlier for bash. Here's the alias I have defined so that I can quickly reload my profile:

alias reloadprofile='source /etc/zprofile && source ~/.zshrc'

I believe you can accomplish the same thing by calling exec zsh, which replaces the current instance with a new one and reloads the .zshrc file, but I haven't experimented enough with it to be completely sure.

  • 1
    I had to modify this a little to get it to work for me. I created two aliases– alias bashr='. ~/.zshrc' and alias loadDefaultPath='. /etc/zprofile'. Then in .zshrc I execute export PATH="", loadDefaultPath, and export PATH="${PATH}". For clarification . is the same as source in the aliases I have defined above. – samurai_jane Aug 24 '22 at 21:55
3

OSX's default $PATH is generated from the contents of the /etc/paths file, it's fairly straight-forward to modify.

  • 1
    Modifying /etc/paths carries the risk of changes getting overwritten by the next OS X update, also this doesn't work for user-specific paths like ~/bin. – nohillside Jun 18 '14 at 07:00
  • That's true, though I usually handle those in my .rc's later. I also haven't had my /etc/paths reset yet by an update (several years now since I started using brew). – Chris Keele Jun 18 '14 at 07:06
  • Currently hacking on a boxen provisioning that I hope will make me impervious to future overwriting updates. – Chris Keele Jun 18 '14 at 07:10
  • 1
    On a single-user system the distinction is somewhat unimportant; but the files in /etc define system-wide defaults, unlike the files in your personal home directory. – tripleee Feb 19 '22 at 09:53
3

Remove the your bash profile to restore the default $PATH. Enter the following command into Terminal.app:

/bin/rm ~/.bash_profile

The change will take place with the next shell or terminal session.

Graham Miln
  • 43,776
  • Answering, based on original questioner's comments, to remove the question from the unanswered queue. – Graham Miln Jan 25 '13 at 12:41
  • 6
    Well, if you want to keep other content of your profile, a simple /bin/mv ~/.bash_profile ~/bash_profile might be better – nohillside Jan 25 '13 at 17:40
  • 1
    This is the simplest answer I have found in the entire SE websites. Thanks! – mr5 Jun 26 '18 at 02:41
  • 1
    Most effective answer for sure – Symphony0084 Mar 25 '20 at 02:39
  • Unfortunately, this is hopelessly and dangerously wrong in the general case. If you only set your PATH in .bash_profile, this is an acceptable solution; but the file has many other uses, and if you use the terminal for any amount of time, you will probably have added other things there which you will not want to lose. – tripleee Feb 19 '22 at 09:51
-1

These methods are great! If you have this problem with zsh, remove ~/.zshrc This will get rid of your zsh settings though.