0

What I noticed is that when I add a :$PATH to the first PATH, it repeats all the path when you echo it.

I use the MANPATH for example, but the same thing happens with the PATH variable.

# I use MANPATH here
export MANPATH="/usr/local/share/man" # Here no :$MANPATH
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"
export MANPATH="/usr/local/opt/make/libexec/gnuman:$MANPATH"

This outputs:

$ echo $MANPATH
/usr/local/opt/make/libexec/gnuman:/usr/local/opt/coreutils/libexec/gnuman:/usr/local/share/man

But when I add :$MANPATH to the first one:

export MANPATH="/usr/local/share/man:$MANPATH"
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"
export MANPATH="/usr/local/opt/make/libexec/gnuman:$MANPATH"

This outputs:

$ echo $MANPATH
/usr/local/opt/make/libexec/gnuman:/usr/local/opt/coreutils/libexec/gnuman:/usr/local/share/man:/usr/local/opt/make/libexec/gnuman:/usr/local/opt/coreutils/libexec/gnuman:/usr/local/share/man:

As you see, all the MANPATH are repeated.

So my questions are:

  1. Should I omit the :$PATH or $MANPATH at the end of the first line?
  2. Why it repeats all the paths when I add it to the end of the first line?
shin
  • 749
  • Just a comment: You only ever need a single export to make a shell variable an environment variable. Exporting it in every assignment is not needed. You don't need to export variables that already are environment variables, like PATH (unless you unset them). – Kusalananda Mar 14 '21 at 11:18

2 Answers2

2

If, when you run

export MANPATH="/usr/local/share/man:$MANPATH"
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"
export MANPATH="/usr/local/opt/make/libexec/gnuman:$MANPATH"

you end up with the content of MANPATH being

/usr/local/opt/make/libexec/gnuman:/usr/local/opt/coreutils/libexec/gnuman:/usr/local/share/man:/usr/local/opt/make/libexec/gnuman:/usr/local/opt/coreutils/libexec/gnuman:/usr/local/share/man:

it means that MANPATH already contained

/usr/local/opt/make/libexec/gnuman:/usr/local/opt/coreutils/libexec/gnuman:/usr/local/share/man:

before the first assignment (i.e. you did not start with a new/empty variable).

As you are showing, foo=bar:$foo means "concatenate the string foo currently expands to to 'bar' and assign the result to foo". You reset the foo variable if you start a series of assignments with foo=bar; you extend foo if you start with foo=bar:$foo.

Coming to the question asked in the title, whether you should use foo=bar (reset) or foo=bar:$foo (extend) depends on the context. Usually, shell profile files that add paths to PATH are meant to only run once per user session. The more conservative choice they can do is to extend PATH, because other programs may have added useful components to it before they run.

If a shell script needs to ensure it only extends an environment variable once, regardless of how many times it is run, it needs to check whether the variable already contains the to-be-added component. For more on this, see How can I cleanly add to $PATH?

fra-san
  • 10,205
  • 2
  • 22
  • 43
1

Doing MANPATH="/path/to/somewhere:$MANPATH" means "add /path/to/somewhere to PATH".
Doing MANPATH="/path/to/somewhere" means "erase whatever MANPATH stores and set it to /path/to/somewhere"
Whether you should do it or not depends on it you want to overwrite or append to the MANPATH variable.

cajomar
  • 113