Last Updated: February 25, 2016
·
251
· robinbb

Elegantly remove a directory from $PATH in Bash shell

removeFromPath() {
  local p d
  p=":$1:"
  d=":$PATH:"
  d=${d//$p/:}
  d=${d/#:/}
  PATH=${d/%:/}
}

Features:

  • is pure Bash,
  • does not invoke other processes (like 'sed' or 'awk'),
  • does not change IFS,
  • does not fork a sub-shell,
  • handles paths with spaces, and
  • removes all occurrences of the argument in PATH.

1 Response
Add your response

Could you give an example how to call this function?

over 1 year ago ·