Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my user environment I/(macports) have set my PATH to be in the form of


However if I have nested shells the PATH variable gets concatenated all the time, of course this is not a major issue, but still, isn't there a better solution?

share|improve this question… – Iain Dec 11 '13 at 10:59

You can check if that directory is already in the PATH pretty easily:

for dir in app1/bin app2/bin; do
    if [[ :"$PATH": != *:"$dir":* ]]; then

It's looking for a colon-delimited directory within your PATH. Note that the asterisks need to be outside the quotes, as shown.

Another way to write it:

[[ :"$PATH": != *:app1/bin:* ]] && PATH="app1/bin:$PATH"
[[ :"$PATH": != *:app2/bin:* ]] && PATH="app2/bin:$PATH"
share|improve this answer
up vote 0 down vote accepted

Ok, the problem is simply that the PATH as-is is still valid for nested shells, i.e. we should not re-evaluate the PATH variable i.e. do not include this in .bashrc|.zshrc, but put this in .profile|.zprofile. Profile configs are run once for every login shell. In this context you can look at the profile configs as being an initial config.

share|improve this answer
If the TS' problem is similar, but the above solution is not viable, then @glennjackman is a very great solution code-wise – hbogert Dec 11 '13 at 22:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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