14

I want to add a path to the PATH environment variable?

I have tried with export PATH=/mypath:$PATH and it works. But the next time I start the Terminal, my new path is not int the PATH environment variable any more.

How can I add a path to the PATHenvironment variable? and it should be there also the next time I start the Terminal.


I have problems with this now again, the trick that worked before doesn't seem to work anymore.

I have tried with:

echo 'export GRADLE_HOME=/Users/jonas/gradle-1.2/' >> ~/.profile
echo 'export PATH=GRADLE_HOME/bin:$PATH' >> ~/.profile

to add two environment variables. Then my ~/.profile-file has this content:

export GRADLE_HOME=/Users/jonas/gradle-1.2/
export PATH=GRADLE_HOME/bin:$PATH

But when I start a new Terminal window and type gradle (the command I added to PATH), I get a message that the command doesn't exists. If I run the command from /Users/jonas/gradle-1.2/bin it works fine!

2
  • If you happen to screw something up, see also: Reset your PATH variable Commented Apr 22, 2011 at 10:29
  • 1
    The assignment to PATH (second export) should be with $GRADLE_HOME
    – nohillside
    Commented Nov 4, 2012 at 21:19

5 Answers 5

6

echo 'export PATH=/my/path:$PATH' >> ~/.bash_profile should do the trick!

If you used echo 'export PATH=/my/path:$PATH' > ~/.bash_profile, or any such variation, you would be overwriting the contents of your profile!

1
  • Note: If it isn't obvious, .bash_profileis a text file and you can also directly open and edit it to add any path either with the command nano ~/.bash_profile in the Terminal or using TextEdit ( superuser.com/a/912645 ).
    – sfxedit
    Commented Sep 8, 2021 at 9:33
13

OSX reads the following files in order when a terminal opens :

/etc/profile
~/.bash_profile
~/.bash_login   
~/.profile     

So place your path addition into one of these. I normally put additions into ~/.bash_profile

2
  • 3
    Thanks, I used echo 'export PATH=/mypath:$PATH' >> ~/.profile
    – Jonas
    Commented Apr 22, 2011 at 8:36
  • Is this still correct now that the default is zsh on Macs? Commented Nov 12, 2023 at 16:46
5

Updated at 2021.0908

I'm using this:

~/.zsh/
├── .fzf.zsh
├── .zcompdump
├── .zprofile              # This will be read before .zshrc.
├── .zsh_functions         # This is for my custom scripts.
├── .zsh_history
├── .zshenv                # (Important) Create a symlink of this to your ~/.
└── .zshrc                 # Put everything in this file is fine. (cont.)
                           # (cont.) this is the ".bashrc" in your head.

Some points:

  • My .zprofile is empty. I prefer to put all stuff inside .zshrc because it doesn't contain too many lines.
  • The content of .zshenv SHOULD contains this line so that we can organize the .zsh family into the ~/.zsh folder, AND don't forget to symlink it into your ~ folder where your OS can read:
ZDOTDIR=/.zsh

DONE. You can ignore my old answer follows.


Old answer: (Not correct. Put everything inside .zshrc is just fine.)

Old question but, for MacOS X Catalina users:

Create/edit ~/.zshenv:

# comment for yourself
PATH="$PATH:to/your/path"

Notice that modify PATH in ~/.zprofile or ~/.zshrc is not correct, it should be ~/.zshenv.

If you need to prepend something to PATH, further create/edit ~/.zprofile:

# apply the prepend to PATH
[[ -r ~/.zshenv ]] && source ~/.zshenv
# remove duplicate in PATH
typeset -U PATH

Reference: http://zsh.sourceforge.net/Intro/intro_3.html

2
  • 1
    What do you mean with "If you need to prepend to the PATH" and why can't this be done in .zshenv as well?
    – nohillside
    Commented Apr 17, 2020 at 6:49
  • In short it's a matter of loading order. path_helper will be executed after ~/.zshenv but before ~/.zproflie. And from my study today I would recommend using symlink instead of modify PATH, which is much simpler.
    – Raining
    Commented Apr 17, 2020 at 8:37
3

In addition to the places mentioned by  @RobZolkos, the login shell also looks in /etc/paths and the files inside /etc/paths.d/. One path entry per line in these files.

See /etc/profile and the manual page for path_helper.

2
  • Ok, thanks! but the biggest problem was how to add the path. But I found echo 'export PATH=/mypath:$PATH' >> ~/.profile
    – Jonas
    Commented Apr 22, 2011 at 10:52
  • @Jonas: I realized that your problem was solved. My answer was intended for the benefit of future readers, so they can see all the options available to them. Commented Apr 22, 2011 at 10:56
0

Insert in .bashrc these lines of code:

function pathadd {              # Add new element to PATH
  if ! echo $PATH | egrep -q "(:|^)$1(:|$)"
  then if [ "$2" = "after" ]
       then PATH="$PATH:$1"
       else PATH="$1:$PATH"
       fi
  fi
}

then type a command like: pathadd /opt/local/bin or pathadd /opt/local/bin after.

1

You must log in to answer this question.

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