Print Values of PATH

Overview

Sources:

Display the $PATH environment variable with each directory on its own line for easier reading.

Code

Bash

echo "${PATH//:/$'\n'}"

Zsh

# for zsh, omit the $ character
echo "${PATH//:/'\n'}"

Alternative Methods

# using tr
echo "$PATH" | tr ':' '\n'
 
# using sed
echo "$PATH" | sed 's/:/\n/g'
 
# using awk
echo "$PATH" | awk -F: '{for(i=1;i<=NF;i++) print $i}'

Details

The Bash syntax ${PATH//:/$'\n'}:

  • ${variable//pattern/replacement} - Replace all occurrences
  • :: - Match colon (the PATH separator)
  • $'\n' - ANSI-C quoting for newline character

Example output:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/home/user/.local/bin

Appendix

Note created on 2025-12-23 and last modified on 2025-12-23.

See Also


(c) No Clocks, LLC | 2025