How to split the contents of `$PATH` into distinct lines?

echo "$PATH" | tr ':' '\n'

Should do the trick. This will simply take the output of echo "$PATH" and replaces any colon with a newline delimiter.

And if you need it in a loop:

for dir in `echo "$PATH" | tr ':' '\n'`; do
    echo "$dir"
done

Note that the quotation marks around $PATH prevents the collapsing of multiple successive spaces in the output of $PATH while still outputting the content of the variable.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)