Monday, June 18, 2012

Linux: pathmunge Command in Shell Script

Q. I see pathmunge used in few scripts under Red Hat Enterprise Linux. Can you explain the use of pathmunge under RHEL / CentOS / Fedora Linux?

A. Red Hat, CentOS, and Fedora Linux has a pathmunge function defined in /etc/profile file. It will add the directories one by one to the default PATH for the root user.
pathmunge() function 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
}
Path manipulation done using pathmunge:
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
fi
As long as you are using Red Hat or CentOS / Fedora Linux, you can use pathmunge. It is best to avoid this function if you need run a shell script on diffrent distributions. I recommend using export bash command for modifying PATH variable.

No comments:

Post a Comment