What is the advantage of pathmunge over grep?
Until 2012, pathmunge used to use grep itself. (Well, egrep, to be precise.)
In Red Hat, CentOS, etc. pathmunge is defined in /etc/profile
:
pathmunge () {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
As you can see, it's slightly more sophisticated than what you propose to do, but not much. The reason your proposal wouldn't work is that without the delimiters it'll make partial matches.
so if my path is /usr/local/sbin:/usr/local/bin
, echo ${PATH} | grep /usr/local
will return true, even though /usr/local isn't in my path. So you need to match the delimiters. But if you grep for :/usr/local:
you'll also fail, because you won't ever match the first or last item in the path, since $PATH neither starts nor ends with delimiter. That's why egrep is used. (^|:) matches either a colon or the beginning of the line. ($|:) matches either a colon or the end of the line.
Modern versions of pathmunge use the shell's built-in pattern-matching capabilities, which is a little more efficient.