PATH=`awk -F: '{for (i=1;i<=NF;i++) { if ( !x[$i]++ ) printf("%s:",$i); }}' <<< $PATH`"$PATH"` Explanation of awk code:
- Separate the input by colons.
- Append new path entries to associative array for fast duplicate look-up.
- Prints the associative array.
In addition to being terse, this one-liner is fast: awk uses a chaining hash-table to achieve amortized O(1) performance.
based on Removing duplicate $PATH entries