tr - Linux


The tr command in Linux is a utility for translating, squeezing, and/or deleting characters from standard input and writing the results to standard output. It is primarily used for transforming text, such as converting lowercase to uppercase, changing line endings, or removing specific characters. tr is most effective for simple text manipulation tasks that can be described by a set of rules for either replacing or deleting characters.


The basic syntax of the tr command is:

tr [OPTION]... SET1 [SET2]

Here, SET1 is the set of characters that tr will search for, and SET2 is the set of characters that SET1 will be replaced with or deleted.


  • -c, -C, --complement: Use the complement of SET1.
  • -d, --delete: Delete characters in SET1, do not translate.
  • -s, --squeeze-repeats: Replace each sequence of a repeated character that is listed in the last specified SET2, with a single occurrence of that character.
  • -t, --truncate-set1: First truncate SET1 to length of SET2.

Each flag modifies the behavior of the command to better suit various scenarios, such as removing repeated whitespace or unwanted characters.


  1. Convert lowercase letters to uppercase:

    echo "hello world" | tr 'a-z' 'A-Z'

    Output: HELLO WORLD

  2. Delete all digits:

    echo "user123" | tr -d '0-9'

    Output: user

  3. Squeeze consecutive identical characters to a single character (e.g., whitespace):

    echo "this    is    a    test" | tr -s ' '

    Output: this is a test

  4. Translate braces into parenthesis:

    echo "function { return true; }" | tr '{}' '()'

    Output: function ( return true; )

Common Issues

  • SET2 is shorter than SET1: When translating, if SET2 is shorter than SET1, unexpected results can occur unless -t is used.
  • Handling non-printable characters: Care must be taken when manipulating non-printable characters, as they may not render as expected in different environments.
  • Locale settings affecting character classes: Characters classes like [:lower:] might behave differently based on locale settings.

Solution: Use the -t option to ensure SET1 is truncated to match the length of SET2. Confirm intended behavior in specific locales.


tr can be combined effectively with other Unix commands in pipelines. For instance, converting a file to upper case and paging through it:

cat file.txt | tr 'a-z' 'A-Z' | less

Here, tr is used with cat and less to create a command chain that enhances readability of file contents.

  • sed: A stream editor that can perform more complex text transformations.
  • awk: A programming language designed for text processing and typically used as a data extraction and reporting tool.
  • grep: A tool for searching text using pattern matching.

For further readings, visit the GNU coreutils manual: GNU tr manual