Skip to content

Instantly share code, notes, and snippets.

@sbamin
Last active November 5, 2021 21:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbamin/a60455897330dce6ec7940e83c927b48 to your computer and use it in GitHub Desktop.
Save sbamin/a60455897330dce6ec7940e83c927b48 to your computer and use it in GitHub Desktop.
Reset PATH in ~/.bash_profile such that PATH variable follows user-defined order of paths.
## add following to the end of ~/.bash_profile assuming ~/.bash_profile takes the precedence over ~/.profile (if present), ~/.bash_profile, or other bash configs.
## for zsh or other shells, you may need to tweak following code.
## Be careful "resetting" PATH variable and know the implications of PATH variable being either empty or missing critical system paths!
################################### SET PATH ###################################
## User specific environment and startup programs
## Rewriting PATH ##
## We want user and anaconda paths to precede system paths, esp. this string:
## /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin which contains
## most of system default libs,
## i.e., incompatible versions of gcc, g++, and other devtools.
## Make sure to append PATH variable in case sysadmin adds more tools in
## /etc/bashrc. This will duplicate most of path but it doesn't matter as we
## set precedence for user configs in PATH string and include all of PATH
## locations.
## Also note ${PATH:+:$PATH} before system paths. This will force system paths to
## append at the end but module paths to precede system paths.
export deprecPATH=$PATH
## Set PATH based on HPC ##
## Sumner is CPU based and Winter is GPU based HPC at JAX.
if [[ "$(hostname)" == *"sumner"* ]]; then
PATH="${HOME}/bin:${SUM7ENV}/bin:${HOME}/anaconda3/bin:${HOME}/anaconda3/condabin:${HOME}/.local/bin:${SUM7ENV}/opt/bin:${SUM7ENV}/local/bin:/cm/shared/apps/slurm/current/sbin:/cm/shared/apps/slurm/current/bin${PATH:+:$PATH}:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin"
MYENV="SUMNER7"
elif [[ "$(hostname)" == *"winter"* ]]; then
PATH="${HOME}/bin:${SUM7ENV}/bin:${HOME}/anaconda3/envs/tf-gpu/bin:${HOME}/anaconda3/condabin:${HOME}/.local/bin:${HOME}/anaconda3/bin:${SUM7ENV}/opt/bin:${SUM7ENV}/local/bin:/cm/shared/apps/slurm/current/sbin:/cm/shared/apps/slurm/current/bin:${SUM7ENV}/opt/gpu/tensorRT/TensorRT-6.0.1.5/bin${PATH:+:$PATH}:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin"
MYENV="WINTER7"
else
PATH="${HOME}/bin:${SUM7ENV}/bin:${HOME}/anaconda3/bin:${HOME}/anaconda3/condabin:${HOME}/.local/bin:${SUM7ENV}/opt/bin:${SUM7ENV}/local/bin:/cm/shared/apps/slurm/current/sbin:/cm/shared/apps/slurm/current/bin${PATH:+:$PATH}:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin"
MYENV="UNKNOWN"
fi
#### remove duplicate PATH entries BUT preserve order of entries ###
## prefer bash solution
## bash: https://unix.stackexchange.com/a/40973/28675
## perl: https://unix.stackexchange.com/a/50169/28675
if [ -n "$PATH" ]; then
## THIS UNSETS PATH - CAREFUL ON CHOICE OF COMMANDS YOU HAVE NOW!
old_PATH=$PATH:; PATH=
while [ -n "$old_PATH" ]; do
x=${old_PATH%%:*} # the first remaining entry
case $PATH: in
*:"$x":*) ;; # already there
*) PATH=$PATH:$x;; # not there yet
esac
old_PATH=${old_PATH#*:}
done
PATH=${PATH#:}
unset old_PATH x
fi
export PATH MYENV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment