Manage PATH Variable in Bash
Posted by Dave Eddy on Sep 12 2018 - tags: techOver the weekend I wrote what can only be described as a completely
over-engineered solution to something that was really nothing more than a minor
annoyance to me. I noticed that some paths in my $PATH environmental variable were
duplicated, and some were there that didn’t exist on the filesystem, and over
all it was just a bit of a mess.
I remember encountering the pathmunge bash function a couple years ago but I
didn’t want something that relied on grep or any external commands - I wanted
to be able to manage $PATH (and $MANPATH) with pure bash.
Introducing bash-path.
Functions to modify colon separated variables like
$PATHor$MANPATH
https://github.com/bahamas10/bash-path
All of the logic is in a single file and can be sourced or copied directly into
a bashrc file to be used. I pulled in bash-path to my dotfiles with this
simple
commit
Example
Given the following environment:
$ . path.bash
$ PATH='/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'
You can add to $PATH using path_add (similar to pathmunge):
$ path_add /my/new/bin
$ path_add /my/new/sbin before
$ echo "$PATH"
/my/new/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/my/new/bin

