Results 1 to 10 of 10

Thread: Duplicate PATH entries

  1. #1

    Default Duplicate PATH entries

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

    Code:
    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?

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

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,998

    Default Re: Duplicate PATH entries

    Because that is what you asked the system to do.

    Code:
    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
    Code:
    /home/user_name/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
    Thus the resulting statement after Parameter expansion becomes:
    Code:
    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.
    Henk van Velden

  3. #3

    Default Re: Duplicate PATH entries

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

    Code:
    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:

    Code:
    export PATH=$PATH:/path/to/dir
    However when I do that they duplicate. Is there a way for me to add them without duplicating?

  4. #4
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,998

    Default Re: Duplicate PATH entries

    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.
    Henk van Velden

  5. #5

    Default Re: Duplicate PATH entries

    Quote Originally Posted by hcvv View Post
    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.
    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.

  6. #6
    Join Date
    Aug 2010
    Location
    Chicago suburbs
    Posts
    12,495
    Blog Entries
    3

    Default Re: Duplicate PATH entries

    Quote Originally Posted by labouie View Post
    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?
    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:
    Code:
    case "$PATH" in
      *:/usr/sbin:*) ;;
      *) export PATH=$PATH:/usr/sbin:/sbin
          ;;
    esac
    so that it doesn't happen twice.
    openSUSE Leap 15.1; KDE Plasma 5;
    testing Leap 15.2Alpha

  7. #7
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,998

    Default Re: Duplicate PATH entries

    Quote Originally Posted by nrickert View Post
    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:
    Code:
    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.
    Henk van Velden

  8. #8

    Default Re: Duplicate PATH entries

    Quote Originally Posted by nrickert View Post
    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:
    Code:
    case "$PATH" in
      *:/usr/sbin:*) ;;
      *) export PATH=$PATH:/usr/sbin:/sbin
          ;;
    esac
    so that it doesn't happen twice.

    Thank you for the information.

  9. #9

    Default Re: Duplicate PATH entries

    Quote Originally Posted by hcvv View Post
    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.
    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.

  10. #10
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,998

    Default Re: Duplicate PATH entries

    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.
    Henk van Velden

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •