Skip to content

Instantly share code, notes, and snippets.

@gongzhitaao
Created July 1, 2016 20:56
Show Gist options
  • Save gongzhitaao/26fd227b520ced8cedc0998fa60053aa to your computer and use it in GitHub Desktop.
Save gongzhitaao/26fd227b520ced8cedc0998fa60053aa to your computer and use it in GitHub Desktop.
Manage Linux environment variables such as PATH, LD_LIBRARY_PATH
# -*- mode: shell-script; coding: utf-8; -*-
pathadd() {
# key
local key=${2:-PATH}
# value of key
local ret=${!key}
# remove trailing slash
ret=${ret%/}
local new=${1%/}
# remove duplicate
if [ -n "$ret" ]; then
tmp=$ret:; ret=
while [ -n "$tmp" ]; do
x=${tmp%%:*} # the first remaining entry
case $ret: in
*":$x:"*) ;; # already there
*) ret=$ret:$x;; # not there yet
esac
tmp=${tmp#*:}
done
ret=${ret#:}
unset tmp x
if [ -d "$new" ] && [[ ":$ret:" != *":$new:"* ]]; then
ret="$ret:$new"
fi
else
ret="$new"
fi
echo $ret
}
# add local bin folder
export PATH=$(pathadd "$HOME/.local/bin")
# add Cask (emacs package manager)
export PATH=$(pathadd "$HOME/.cask/bin")
# add cuda path
export PATH=$(pathadd "/usr/local/cuda/bin")
export LD_LIBRARY_PATH=$(pathadd "/usr/local/cuda-8.0/lib64" LD_LIBRARY_PATH)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment