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 manipulation done using pathmunge:

# Path manipulation
if [ "$EUID" = "0" ]; then
        pathmunge /sbin
        pathmunge /usr/sbin
        pathmunge /usr/local/sbin

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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • Patrick Horgan Aug 13, 2008 @ 21:43

    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:
    pathmunge /usr/bin
    will do nothing.
    pathmunge /sbin
    will result in a path a path containing:
    if instead, you’d said pathmunge /sbin after, the
    result would have been:
    For portability, if you need something like this, just make it a part of your script.

    • gema Jan 13, 2015 @ 17:13

      A voice from the future… :) thank you Patrick! I had just asked myself, ‘ok, but what does pathmunge do?’ and then I scrolled down and found your explanation.
      Much appreciated!

  • dt Aug 14, 2008 @ 4:14

    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.

  • haneen Oct 7, 2012 @ 0:43

    how to do this task ::Assume a directory contains many .mp3 files taken from albumes. If you are asked to sort them out into separate folders by artist. How would you do that?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum