Skip to content

Instantly share code, notes, and snippets.

@willurd
Last active May 15, 2023 06:12
Show Gist options
  • Save willurd/5648907 to your computer and use it in GitHub Desktop.
Save willurd/5648907 to your computer and use it in GitHub Desktop.
Manage your PATH with a nice, one-directory-per-line file, rather than a gargantuan blob of colon-delimited text.
# Read the contents of ~/.path into $PATH, if ~/.path exists. ~/.path should be a file
# consisting of one path on each line, such as:
#
# ~$ cat ~/.path
# # vim: ft=sh
# ~/usr/bin
# /opt/local/bin
# ... etc ...
#
# Note that comments begin with a hash (#).
#
# awk-fu courtesy of pickledspiders:
# http://www.reddit.com/r/linux/comments/1f1kd8/manage_your_path_with_a_nice_onedirectoryperline/ca5ww5d
# awk line fix courtesy of cpitchford:
# http://www.reddit.com/r/linux/comments/1f1kd8/manage_your_path_with_a_nice_onedirectoryperline/ca61un7
DOT_PATH_FILE=~/.path
if [ -e "$DOT_PATH_FILE" ]; then
export PATH=$PATH:`awk '/^[^#]/{printf "%s",(++x!=1?":":"")$0}' $DOT_PATH_FILE`
fi
@vodik
Copy link

vodik commented May 27, 2013

Probably better to change the first line to this:

DOT_PATH_FILE="${DOT_PATH_FILE:-$HOME/.path}"

So someone can override the loopup by setting DOT_PATH_FILE before calling the script.

This is a litte better for the rest:

if [[ -e "$DOT_PATH_FILE" ]]; then
    export PATH="$PATH:$(awk '/^[^#]/{printf "%s",(++x!=1?":":"")$0}' "$DOT_PATH_FILE")"
fi

And for what its worth, ~ isn't a valid character in ~/.path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment