Skip to main content
  1. Notes/
  2. Bash/

setting path

bash - This article is part of a series.
Part 3: This Article

PATH
#

To customize the configuration of our shell the first part is to get our path settings under control. In my own setup I’ve chosen to add (or re-add) the pathmunge function found in the default configuration files.

pathmunge() {
  local dirn="$1"
  [[ -z "${dirn}" ]] && return
  [[ -d "${dirn}" ]] || return

  if echo "$PATH" | grep -E -q "(^|:)$1($|:)"; then
    return
  fi

  if [[ "$2" = "after" ]]; then
    PATH=$PATH:$1
  else
    PATH=$1:$PATH
  fi
}

Adding this makes it easier to update the PATH variable without too much hassle.

pathmunge "/bin"
pathmunge "/usr/bin"
pathmunge "/usr/local/bin"

pathmunge "/sbin"
pathmunge "/usr/sbin"
pathmunge "/usr/local/sbin"

pathmunge "${HOME}/bin" "after"
pathmunge "${HOME}/.local/bin"

I also use path_clean to cleanup the path a bit.

# If needed dedup the PATH entries
if command -v path_clean >/dev/null 2>&1; then
  PATH="$(path_clean)"
fi
bash - This article is part of a series.
Part 3: This Article