Duplicate PATH entries

Hi I’ve noticed a quirk with the PATH variable. In my .bashrc file I added 2 entries (/usr/sbin & /sbin).


export PATH=$PATH:/usr/sbin:/sbin

Afterwards, when I type echo $PATH, I notice the items I added in my .bashrc appear twice. This happens for any/all additional directories I add to my PATH variable in my .bashrc Any insight into why?


user_name@Tumbleweed:~> echo $PATH
/home/user_name/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/sbin:/sbin

Thanks.

Because that is what you asked the system to do. :slight_smile:

export PATH=$PATH:/usr/sbin:/sbin

First thing that happens is that $PATH is replaced by the string that is the value of PATH at that moment (this is called Parameter expansion in the man page of bash)
There is a start value of PATH and it seems to be

/home/user_name/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Thus the resulting statement after Parameter expansion becomes:

export PATH=/home/user_name/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/sbin:/sbin

And that is duly done.

I have no idea why you want to add these two system directories to the PATH variable, because, as you see, they are, not surprising, already part of the default value of PATH.

Thanks for the reply hcvv but I should have been more clear. Directories /usr/sbin & /sbin do NOT appear by default in my $PATH.


user_name@Tumbleweed:~> echo $PATH
/home/user_name/bin:/usr/local/bin:/usr/bin:/bin

Thus I add them to my .bashrc file. Every instruction I’ve read on adding directories to your $PATH says to do the following in .bashrc or .profile:

export PATH=$PATH:/path/to/dir

However when I do that they duplicate. Is there a way for me to add them without duplicating?

My you should first think over why they are not by default in, what you call, “my” PATH.
I assume that that is the path of a “normal” user and not root, because the default PATH of root does have them. Maybe that is for a reason?

In any case, the statement you use is in fact a string expression where you add a string at the end of an existing string. When that is done again, it is added again, and again. and …

Thus it seems that for some reason you “went trough” .bashrc twice. Which is quite possible when you start a new bash from a running bash. After all the environment variables are inherited by the child process and .bashrc is added for every bash start.

.profile is only used at login to a shell and not at every start. Maybe you beter put it there.

Having them twice is of course only a minor performance loss, but having them 60 times …

Coming back to my first remark above, I have no idea why an end-user should wish to have those two libraries (that contains executables he most probably can not execute) in his PATH. But you failed to present the case. So further advice on what you should do is a bit difficult.

You are correct. When I go to a terminal (Alt + Shift + F2) and type echo $PATH I do NOT see duplicates. However, when I go to a terminal emulator (i.e. Konsole, Yakuake, etc…) and type echo $PATH I see the duplicates. So it’s something about those programs that seem to be the issue.

Seems like strange behavior indeed as I do most of my work on a terminal emulator. But thanks for pointing me in the right direction.

Your “.bashrc” is likely being used as part of your desktop session startup. So when you then open a terminal, it is used a second time.

You need something like:


case "$PATH" in
  *:/usr/sbin:*) ;;
  *) export PATH=$PATH:/usr/sbin:/sbin
      ;;
esac

so that it doesn’t happen twice.

Yes, that would be one way to avoid double (or more) additions.

But it does not answer the question why to add these particular directories to an end-user’s PATH. An end-user shouldn’t need those. :frowning:

Thank you for the information.

My preferred method of shutting down my laptop is via Konsole and typing either reboot or poweroff, which reside in /sbin. I simply type poweroff or reboot instead of /sbin/shutdown or sbin/reboot. I can probably create an alias to do the same but there may be other items in /sbin or /usr/sbin I’d like access to w/o typing the whole path. For instance accessing yast, xfs_repair, etc… via console. I’m the only user on my laptop thus it’s just a preference.

A quick look, but as far as I can see these are all to be executed by root, not by the end-user. And root has those already in his default PATH.