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/tools:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
Just change that first one to this:
[[ -z "$PATH_ORIGINAL" ]] && export PATH_ORIGINAL=$PATH
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

Followers

About Me

My photo

Believe it or not, I've never been to jail. Not even for a night. I quit going to bars by the time my fake ID was unnecessary, and I quit hacking into businesses and governments once they started paying me.

Now I just provide for my family and try not to notice politics. I get too angry when I watch how much of my income is taken from me, how it is used, and who is doing it. For the same reason, I can't bear to acknowledge celebrities.

After high school, I went to U of K but the outdated computer engineering curriculum and the seemingly endless amounts of money to be made with my internet specific skills drew me to larger tech markets. I have worked in Silicon Valley, New York City, and have settled in Atlanta.