Translate (tr) Examples

May 11, 2025


Squeeze white spaces.

The following will replace a sequence of repeated character with a single character.

echo "Mary had  a  little   lamb." | tr -s " "


Split input.

The following will translate (convert) a whitespace to a newline.

ls | tr " " "\n"

This is similar to

ls | sed -e 's/ /\n/g'`


Join input.

The following will translate (convert) a newline to a whitespace.

ls -1 | tr "\n" " "