skip to Main Content

I started looking into files such as:

  • /etc/profile
  • ~/.bash_profile
  • etc.

in order to locate where environment variables were defined. Unfortunately, I couldn’t locate the $PATH variable. I am using Bash.

2

Answers


  1. The initial PATH environment variable is inherited from … whatever launched the shell. For example commands like sudo, sshd, whatever creates your shall after a desktop login.

    There also appears to be a PATH that is hardwired into the bash binary for cases where an initial PATH is not inherited. (Look at the output from strings /bin/bash.)

    Then various shell initialization scripts get a go at setting or updating PATH. For example, on Ubuntu the PATH variable is updated by /etc/profile.d/apps-bin-path.sh … which is run by /etc/profile.

    Login or Signup to reply.
  2. You should not worry (or even ask) where PATH is set, since you should not be trusting a random distro to put the right directories in the right sequence.

    Instead, you set the PATH you need in your shell’s profile. That’s it.

    As a starting point, POSIX mandates that getconf PATH returns the system’s default PATH. If you have a $HOME/bin and there’s a /usr/local/bin, then you add them.
    Here’s what this looks like on my machine:

    PATH="$(/usr/bin/getconf PATH)"
    PATH="$PATH:/usr/sbin"
    PATH="$PATH:/usr/local/bin"
    PATH="$PATH:$HOME/bin"
    

    With this setup, it’s easy to adapt the sequence. Maybe you don’t like the ancient vim in /usr/bin/vi? Compile it yourself and move /usr/local/bin to the front.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search