Modify your PATH on Linux

Normally this only comes up these days if you are compiling your binaries or have downloaded some out side your package management system.   I come from Solaris so I like to use /opt for all my self compiles and I prefix that with my initials ie /opt/mwd/bin this way I know it’s software I’ve compiled 6 months down the track when I am cursing it’s not got support for something I now need.

I use a multitude of linux distributions based on the hardware or purpose of the machine in question, ie servers are normally centos for greater vendor support or debian, and opensuse for my desktops.. I find this method works more or less for all the different linux flavours kicking around.  I am assuming like most people bash is the shell of choice.

As most will know the PATH environment is a variable that get set every time you spawn a new session, login or source it by a group of system configuration files.   These configuration files are basically read in the following sequence;

  • /etc/profile,
  • then every /etc/profile.d/*.sh file that your user has access to,
  • then /etc/profile.local if it exists (by default it won’t),
  • and finally by $HOME/.bashrc and $HOME/.profile

Every time you spawn a session or shell, the new shell process will inherit all the environment variables that have been provided by the configuration files above, what I mean is they have a flow on effect in that no one files dismisses the previous they build on each other.

When it comes to setting your personalised PATH environment you really don’t want to use a configuration location that will get clobbered in system updates so /etc/profile is out, don’t touch it.  I also don’t like using $HOME configuration files as I have users on my systems that I generally want to make this software available to and since /etc/profile.local doesn’t exist by default and will be globally read by all users on the system I choose it 🙂

If it was just for your user, then apply the change to $HOME/.profile other wise do as I do and apply the changes to /etc/profile.local.

I find the quickest way to achieve this is to simple issue the following command;

echo ‘PATH=/opt/mwd/bin:$PATH’ >> /etc/profile.local

(remember mwd are my initial this could easily be foo)

I would like to mention that in my example here we are appending to /etc/profile.local by using “>>” instead of just “>” as this creates / overwrites existing files.  This is a safety feature, as on some distributions this file may already exist and we might not want to blindly clobber it.  Also as stated previously the process inherits any variable that has been set previously by an earlier configuration file so in this case we do not need to “export” our new path.

Once you have made this chance you most likely won’t feel like having to respawn your session or logout and back in, so we’ll just source our new configuration with;

source /etc/profile.local

It’s that simple, one last thing.. if your linux system only has one user and that user is you and you are compiling your own stuff it may be easier just to install to $HOME/bin as most modern distro’s will read this path already in the default profiles.

Leave a Reply