Print the paths in $PATH sorted by line length

echo "${PATH//:/\\n}" | awk '{print length, $0}' | sort -n | cut -f2- -d' '

March 7, 2020tg-z

Explanation

For example given PATH=/usr/bin:/bin, it would print:

/bin
/usr/bin
  • echo "${PATH//:/\\n}" replaces the : separator in $PATH with a newline, so that we have one path per line
  • | awk '{print length, $0}' prints for each line in the input the length of the line and the line itself, separated by a space
  • | sort -n sorts the input numerically (instead of the default: alphabetically)
  • | cut -f2- -d' ' prints the second and all following fields of the input (-f2-), delimited by a space (-d' ')