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.
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- My 10 UNIX Command Line Mistakes
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
- Email FAQ to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: 08/12/08




{ 2 comments… read them below or add one }
It would have been nice if you explained what the script does! pathmunge adds something to the path only if it wasn’t already there. So if your path contains:
/bin:/usr/bin
pathmunge /usr/bin
will do nothing.
pathmunge /sbin
will result in a path a path containing:
/sbin:/bin:/usr/bin
if instead, you’d said pathmunge /sbin after, the
result would have been:
/bin:/usr/bin:/sbin
For portability, if you need something like this, just make it a part of your script.
I agree, you didn’t really answer much of the question at all. Also, it is not ‘best to avoid this function’ as you state. What is best is follow Patrick’s advice and just copy it directly into your own shell script.