How to Display a Neatly Formatted Path

September 08, 2013

The following function displays your PATH environment variable using color coding and showing each path entry on its own line.

    path() {
      echo $PATH | tr ":" "\n" | \
        awk "{ sub(\"/usr\",   \"$fg_no_bold[green]/usr$reset_color\"); \
               sub(\"/bin\",   \"$fg_no_bold[blue]/bin$reset_color\"); \
               sub(\"/opt\",   \"$fg_no_bold[cyan]/opt$reset_color\"); \
               sub(\"/sbin\",  \"$fg_no_bold[magenta]/sbin$reset_color\"); \
               sub(\"/local\", \"$fg_no_bold[yellow]/local$reset_color\"); \
               sub(\"/.rvm\",  \"$fg_no_bold[red]/.rvm$reset_color\"); \
               print }"

Here’s a sample of the output:

Image of formatted path

I find this function to be particularly useful when dealing with RVM and Homebrew as they are both particular about where in the PATH they occur.

Author's profile picture

Mark H. Nichols

I am a husband, cellist, code prole, nerd, technologist, and all around good guy living and working in fly-over country. You should follow me on Twitter.