2

I have added entries such as the following in my /etc/bashrc (on Fedora).

#=========== Maven Related variables
export JAVA_HOME='/usr/java/default'
export PATH=${JAVA_HOME}:$PATH


#=========== Maven Related variables
export M2_HOME=/usr/local/apache-maven/apache-maven-3.0.4
export PATH=${M2_HOME}/bin:$PATH

#=========== Ant Related variables
export ANT_HOME=/usr/local/apache-ant
export Path=${ANT_HOME}/bin:$PATH

Now, each time that I execute bash command to refresh the environment variables, all these additions are repeated, and the PATH just keep adding itself recursively; if I keep doing bash for a few dozen times, then the $PATH becomes a hundred lines of repeating content. What am I doing wrong?

Note that I have added these entries to /etc/bashrc since I want to have these values in PATH no matter what user I login as.

Thanks, Shannon

user1888243
  • 2,591
  • 9
  • 32
  • 44
  • What command do you _"execute ... to refresh the environment variables"_? – Jim Garrison Dec 12 '12 at 00:08
  • This is what happens if you include a $PATH var in the value of your PATH= assignment. Prior contents get brought in. If you "source" your profile multiple times, then the var accumulates. This isn't a PATH var specific thing; you can try it with any var. I agree that if you manage your PATH on one big line, it gets annoying. But in your case, just finding where your PATH is set, then adding "${JAVA_HOME}:${M2_HOME}/bin:${ANT_HOME}" to it, would solve/avoid the issue. – Scott Prive Jul 21 '20 at 22:51

4 Answers4

5

Don't set your PATH incrementally in .bashrc; set it once in .profile and leave it alone thereafter. Or, since you mention /etc/bashrc, don't set the PATH incrementally in /etc/bashrc; set it once in /etc/profile and leave it alone.

One side-benefit; things will work a little faster.

See also the code in How do I manipulate PATH elements in shell scripts for code to clean up a repetitive PATH.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • FWIW, if anything in `/etc/bashrc` isn't instantaneous and pain-free, it doesn't belong there. The `c**p` in the `/etc/bashrc` file on the work machines (where the `**` represents an 'a' and an 'r', and I'm not discussing fish) doesn't bear thinking about; I don't use `bash` at the office in part because of the nonsense which I don't want them doing to me. (Amongst other things, it changes my `umask` from what I want to what they think is better; I object — their decision is ... less than desirable!) – Jonathan Leffler Dec 12 '12 at 00:16
  • 1
    More simply: it is bad practice to use /etc/profile to set customizations which really only apply to a single user. This is true even if the OS hosts only one user. Specific user's info doesn't belong in /etc. – Scott Prive Jul 21 '20 at 22:45
3

If by this statement:

... execute bash command to refresh the environment variables ...

you mean that you are entering the command

bash

at the command prompt, you are not "refreshing the environment variables". You are launching a new subshell of the current shell. The new shell inherits the path of the original shell, to which you are once again making additions. Each time you do this the PATH will get longer.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

You can use something like:

PATH=$(echo "$PATH" | awk -v RS=: -v ORS=: '!(a[$0]++)' | sed 's/:$//')

to clean up your path after changing it. Also, since the the first match is used when scanning the path, having duplicates doesn't really matter.

perreal
  • 94,503
  • 21
  • 155
  • 181
0

I had also faced the same problem (CentOS). This is how I fixed it.

  1. Added the following lines to my user's .profile

    export PATH=/usr/local/apache-maven-3.3.3/bin:$PATH

    export JAVA_HOME=/usr

    export SHELL=/bin/bash

    # to run bash (because ksh was my default shell)

    /bin/bash

  2. No changes to my user's .bashrc file

  3. No changes to /etc/profile
  4. No changes to /etc/bashrc
Sanjay Das
  • 180
  • 3
  • 14