How do I reset the $PATH variable on Mac OS X?

130,928

Solution 1

If you have a ~/.MacOSX/environment.plist file, check it to see if it provides a default PATH value.

If it is in XML format (plists can be in many formats), you can edit with any text editor. Check it with plutil -lint ~/.MacOSX/environment.plist if you edit it by hand.

Or, you can use commands like defaults or PlistBuddy to make controlled modifications to XML or binary format plist files.


You can always set your own PATH in any of your shell's initialization files.

Put something like the following in your of your shell's startup files (.bashrc, or .bash_profile/.bash_login/.profile):

PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

# add custom, local installations to PATH
PATH=/usr/local/bin:/usr/local/sbin:"$PATH"

# add MacPorts to PATH
PATH=/opt/local/bin:/opt/local/sbin:"$PATH"

That will override whatever default PATH is set when the shell starts (the first PATH= does not use $PATH, so it will always start out with only whatever you give it).

Only one of the ‘login’ files will ever be used (the first one that exists and is readable of ~/.bash_profile, ~/.bash_login, and ~/.profile will be used). .profile is for backwards compatibility with other shells—if you use it, be sure to keep it free of syntax that is specific to bash. If you go with .bash_login or .bash_profile (they are functionally equivalent except for the names), then use a line like [[ -e ~/.bashrc -a -r ~/.bashrc ]] && source ~/.bashrc ]] near the top so that login shells will also get the customizations made in your .bashrc.

If you want all instances of bash to have the same PATH, then use .bashrc. If you often find yourself interactively modifying a single shell's PATH from the command line and want to use that modified PATH in subshells (a cases that is probably not terribly common), then you should put the statements in one of the ‘login’ files instead. Pick only one of the login files and use it.

Solution 2

1.Open your terminal

2.You could firstly just check your current $PATH, type

echo $PATH

to terminal

3.If the $PATH that terminal gave back is the path you want, then you are good; if not, type

export PATH=/usr/bin:/bin:/usr/sbin:/sbin

, then type

touch ~/.bash_profile

, and then type

open ~/.bash_profile

, you will then find a EditText open up, now type the path you want in that EditText; For instance, type

PATH=/usr/bin:/bin:/usr/sbin:/sbin

(which is the default $PATH for mac, considering you want to reset the $PATH so you should probably type this), save it, and then close EditText, then close Terminal.

4.Now you have already changed your $PATH or reset to the mac's default $PATH!

Solution 3

Chris Johnson's rc/profile edits shown also work fine in /etc/profile.

I just appended:

export PATH="/usr/local/mariadb/bin:$PATH"

to my /etc/profile file in Mac OS X 10.9 Mavericks to complete the MariaDB install (which works as a great drop-in replacement for MySQL).

Solution 4

If you use ZSH - the path variable seems to thankfully recover when you reboot your terminal session.

https://github.com/robbyrussell/oh-my-zsh

https://www.iterm2.com/

Share:
130,928

Related videos on Youtube

Stian Tofte
Author by

Stian Tofte

Updated on September 17, 2022

Comments

  • Stian Tofte
    Stian Tofte over 1 year

    I've messed up my path variable, and now some apps that I run raise errors saying Command Not Found (error 127) for commands like date and sleep. These commands work fine when executed directly in the shell.

    I'm guessing this has something to do with a malformed $PATH variable, and need to know how to reset it. I've deleted the files ~/.bashrc, ~/.bash_profile, /etc/bash.bashrc, and ~/.bashrc and ~/.profile.

    What other files could hold my $PATH? Is there some simpler way to reset the Path than dig into the myriad files which could hold my path?

    Note, this path problem is only with my user. I made a test user on my system, and the path was fine, back to normal.

    UPDATE: Thanks. I dunno which one of the files I deleted did it, but things are working again. You guys did what the 'Experts' couldn't. And yes, Chris, you were right. The PATH customizations I had made were in bash_login. But somehow it worked without me deleting those customizations. I think it might have been coz I was using this prefpane called 'RCEnvironment', and I had entered a path with quotes and :PATH in it. I dunno whether it takes quotes, and it doesn't replace :$PATH, so that probably is the root of the error. I forgot I even had that prefpane!

  • Arjan
    Arjan about 9 years
    I feel there is a lot above that has no effect. Why do you think touch is required? Also, as you're closing Terminal anyway, why run the export on the command line, rather then only adding it to ~/.bash_profile (which needs export too) and be done with it? (Alternatively, run source ~/.bash_profile to apply the changes to the current window.)
  • ThinkCode
    ThinkCode over 6 years
    True, some of it is redundant. Saving whatever PATH value to .bash_profile should do.
  • John
    John over 3 years
    I've been looking everywhere for this solution. Whenever I used source .bash_profile that command was spawning copies of the paths I had added to .bash_profile using export instead of checking to be sure the paths were added. One thing, what do you keep in .bashrc that shouldn't go in bash.profile?
  • John
    John over 3 years
    [[ -e ~/.bashrc -a -r ~/.bashrc ]] && source ~/.bashrc ]] is giving me an error on Mojave: -bash: .bash_profile: line 5: syntax error in conditional expression -bash: .bash_profile: line 5: syntax error near -a' -bash: .bash_profile: line 5: [[ -e ~/.bashrc -a -r ~/.bashrc ]] && source ~/.bashrc ]]
  • John
    John over 3 years
    Noticed that your solution will exclude any paths appearing in /etc/paths.d.
  • Charlie Parker
    Charlie Parker over 2 years
    how do I permenantly overwrite PATH in the command line? export PATH didn't seem to do the trick.