Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

PATH=app1/bin:app2/bin/:...:$PATH

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
    
superuser.com/questions/39751/… –  Iain Dec 11 '13 at 10:59
add comment

2 Answers

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
        PATH="$dir:$PATH"
    fi
done

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
add comment
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
add comment

Your Answer

 
discard

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.