codrut.pro
  • CV
  • Projects
  • Snippets
  • Tags
  • More

Prepend to $PATH in bash

October 1, 2022 One-minute read
linux • bash

You should always check if the value is present in a list before adding it in.

function prepend_path () {
  case ":${PATH}:" in
    *:"$1":*)
    ;;
    *)
      PATH="$1:${PATH}"
  esac
}

Usage Link to heading

prepend_path ~/.local/bin

Cleanup Link to heading

unset prepend_path

See also in Bash Tricks Link to heading

  • Pretty print Linux $PATH