Last Updated: February 25, 2016
· otakup0pe

Sane PATH building while reloading .profile

For various reasons I have a .profile workflow that involves the file itself being built from multiple templates depending on the workstation usage. As an example, on some computers I don't care about certain AWS keys or ruby development. This necessitates the dynamic building of my PATH variable as different templates may or may not need to modify it either directly or via an included script/eval.

I also tend to be constantly working on my .profile and am thus reloading it throughout work sessions. Early on in this process I noticed that, as my PATH variable was constantly rebuilt some aspects of it were being duplicated.

The following snippet was my approach to this woe.

function path_add
    if [ $# == 2 ] ; then
    echo $PATH | grep -e "${1}" &> /dev/null
    if [ $? == 1 ] ; then
    if [ "$DIR" == "end" ] ; then

Now in my .profile the PATH variable is only ever explicitly set once to the barest of defaults. Any other time I need to manipulate the PATH it happens via the path_add function.

path_add $HOME/bin start
path_add "/opt/X11/bin"

I look forward to others telling me how terrible (or wonderful) this solution to a problem (that may not even be real) actually is