An occasional outlet for my thoughts on life, technology, motorcycles, backpacking, kayaking, skydiving...

Friday, February 27, 2015

Make your .profile or .bashrc idempotent.

If you are like me, you have about a dozen lines in your .profile the prefixes directories onto your $PATH environment variable. Have you ever noticed how nasty your $PATH variable gets after a few sub shells or re-sourcing your .profile after and edit? I just added an alias and then did...
. ~/.profile
...and boy things where ugly in the $PATH. So, how do you make things idempotent (able to be called mutliple times without side effects)? Well, your first modification to $PATH is probably something like:
export PATH=~/bin:$PATH
Then you probably have a bunch of others like:
export PATH=/usr/local/heroku/bin:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
Just change that first one to this:
export PATH=~/bin:$PATH_ORIGINAL
That means you create $PATH_ORIGINAL once and only once. Then you use it as the starting point for all your path appendages later.

Try doing this before and after the change and see what you get... You may be shocked by the before.
echo $PATH
for i in $(seq 10); do source .profile; done
echo $PATH

No comments:

Post a Comment