1

I am trying to modify the PATH variable in two different scripts located in /etc/profile.d/

  • /etc/profile.d/php.sh

    #!/bin/sh
    PATH=$PATH:/usr/local/php/bin
    export PATH
    
  • /etc/profile.d/mysql.sh

    #!/bin/sh
    PATH=$PATH:/usr/local/mysql/bin
    export PATH
    

Restarting CentOS and checking the PATH variable (edited), the $PATH is modified twice!

[]$ echo $PATH
/usr/lib/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:
/usr/local/mysql/bin:/usr/local/php/bin:/home/IntUser/bin:/usr/local/mysql/bin:
/usr/local/php/bin
[]$ 

In root mode:

[]$ su
Password: 
[]# 

The $PATH is modified 3 times!

[]# echo $PATH
/usr/lib/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:
/usr/local/mysql/bin:/usr/local/php/bin:/home/IntUser/bin:/usr/local/mysql/bin:
/usr/local/php/bin:/usr/local//mysql/bin:/usr/local/php/bin
[]# 

Why is it not assigned only one time?

2
  • Try to trace bash start up: bash -lvx -c true >log 2>&1. See log to find where it updates PATH. Commented Jul 5, 2015 at 4:42
  • Does PATH already contain those directories before you add them? Also, each new bash shell loads the profile scripts, so that's why you see them added to the end again Commented Jul 5, 2015 at 15:25

2 Answers 2

1

Here's the fix: replace your file /etc/profile.d/php.sh contents with:

pathmunge /usr/local/php/bin after

and similarly for the file /etc/profile.d/mysql.sh:

pathmunge /usr/local/mysql/bin after

The explanation is that the files in /etc/profile.d/*.sh are sourced by /etc/profile (which is itself sourced by a login bash shell).

Because the files are sourced, it is as they were part of /etc/profile so they can use any variables and functions in that file. pathmunge is such a function (on my fedora 20 anyway), whose sole purpose is to add a new directory to the PATH, but only if it is not already there. The keyword after adds the new directory to the end of the PATH.

Hence you will have no duplicates. However, I do not know why you have them in the first place.

1
  • Yes, pathmunge was the solution! Commented Jul 9, 2015 at 1:20
0

This happens because something in your configuration is loading /etc/profile multiple times. You need to locate it and fix it.

The file /etc/profile is read by a login shell. It's also read when logging in to a graphical session on many platforms (but it depends on the display manager, on the session manager, and on how your distribution set them up).

One possible problem is that you're requesting /etc/profile to be read from some other location such as ~/.bashrc. If that's the case, remove it. Run grep -s profile ~/.* to see potential offenders. Since running su reads /etc/profile, there has to be at least one instance of that problem, probably with .bashrc.

Another possible problem is if you run a login shell when you open a terminal in a GUI session. The fix is then not to run a login shell — you're already logged in to the GUI session, the terminal is an ordinary application.

Given that you have one occurrence of the directories before /home/IntUser/bin and another one after, /etc/profile is getting read a second time after the part that adds /home/IntUser/bin.

Alternatively, if you don't want to fix your configuration, you could add the directory to the PATH only if it isn't there already.

case :$PATH: in
  *:/usr/local/php/bin:*) :;;
  *) PATH=$PATH:/usr/local/php/bin;;
esac
1
  • I was testing your script, bhut the result is equal... Commented Jul 9, 2015 at 1:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.