I want to edit my bash $PATH globally for all shells. I was told that I could add/remove stuff from my .bash_profile or .bashrc to do this, but when I look in these files, they are missing folders that I see when i

echo $PATH

Is there a way to edit the $PATH globally for all shell sessions from the terminal itself? Or is there another way to configure this? This is my path:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/users/developer/desktop/stack/3.dev-ops/build-test-deploy/front-end-package-manager/composer/bin:/usr/local/go/bin:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Wireshark.app/Contents/MacOS:/Applications/Xamarin Workbooks.app/Contents/SharedSupport/path-bin

I am trying to remove this:

  • 1
    What do you mean for "all shells"? Bash is your shell. If you want to edit your path for other shells like CSH or ZSH then you'll need to modify the environment variables specific to those shells. – A H Sep 4 '18 at 4:03
  • I meant for every time I open iterm2. – Cody Rutscher Sep 4 '18 at 4:07
  • or terminal.app for that matter. – Cody Rutscher Sep 4 '18 at 4:07
  • Are you running iterm as the root user? To find out: execute whoami at the iterm prompt. – Isaac Sep 4 '18 at 4:25
  • 1
    @Isaac No, they are part of the standard part of PATH on e.g. macOS (which I have reason to believe that the user is using). – Kusalananda Sep 4 '18 at 6:20

BASH first reads and executes commands from the file /etc/profile, if that file exists. After that it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile in that order, and reads and executes commands from the first one that exists and is readable.

So your PATH variable is either being set in /etc/profile or from .bash_profile, .bash_login, or .profile from your home directory.

If you want to add or remove directories to your path you should look there. If your path is being set globally from /etc/profile I'd recommend setting your custom PATH locally in your ~/.bash_profile. It's better not to change the global /etc/profile file.

EDIT: It looks like you were using macOS so the answer is to edit your /etc/paths file and remove the offending directory.

Make sure to explain you're running macOS in future questions to save time, as their configuration is a little different than e.g. Linux.

  • what's odd is when I check all of those locations, none of them have that path that I am trying to remove. – Cody Rutscher Sep 4 '18 at 4:14
  • It's being set from somewhere in config file in your home directory, there's no other possibility. Did you try grepping for the path you're looking for? – A H Sep 4 '18 at 4:17
  • no how do I do that? – Cody Rutscher Sep 4 '18 at 4:17
  • the path doesn't exist anymore by the way – Cody Rutscher Sep 4 '18 at 4:17
  • but it is still in my echo $PATH which is strange. – Cody Rutscher Sep 4 '18 at 4:18

The PATH could be set in any of the various files bash reads when it starts. To find them all, run this in a new instance of iterm:

grep --color -rlH 'PATH=' \
     ~/.bashrc ~/.profile ~/.bash_profile ~/bash.login \
     ~/.bash_aliases /etc/bash.bashrc /etc/profile \
     /etc/profile.d/* /etc/environment \
     ~/.xinitrc ~/.xprofile /etc/xprofile \
     /etc/paths /etc/paths.d/
                        2> /dev/null

Those are the standard ones. If you are also sourcing other files from one of those, things can get more complex.

If you find the file setting the PATH variable then you are done and can edit such file. If not, maybe you have removed some packages. What is the reported PATH in a new instance of iTerm?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.