Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux tr command

Reference for the Linux tr command: translate, delete and squeeze characters, character classes like [:upper:], complements, and why tr works on bytes.

tr reads standard input and rewrites it one character at a time. It can translate characters from one set to another (upper to lower case, spaces to newlines), delete characters, or squeeze runs of a repeated character into one.

tr takes no file arguments: it always reads standard input and writes standard output, so use a redirect or a pipe. It is not a substitute for sed, because it cannot match words or patterns, only individual characters.

Terminal
tr [OPTION]... SET1 [SET2]

Try it

Bash Translate and delete

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Squeeze, split and complement

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
tr [-cCds] SET1 [SET2]
tr -d [-c] SET1
tr -s [-c] SET1
Option / form Description
tr SET1 SET2 Translate each character of SET1 into the character at the same position in SET2.
-d Delete characters in SET1 (no SET2).
-s Squeeze each run of a repeated character from the last given set into one.
-c, -C Use the complement of SET1: everything not in it.
-t Truncate SET1 to the length of SET2 instead of padding SET2.

Set notation:

Notation Meaning
a-z A range, in character-code order.
[:alpha:], [:digit:], [:alnum:], [:upper:], [:lower:], [:space:], [:punct:], [:blank:] Character classes.
\n, \t, \r, \\, \NNN Escapes; \NNN is an octal byte.
[c*n] Repeat c n times (in SET2); [c*] repeats to fill the length.

Returns: 0 on success, non-zero on invalid sets or I/O errors.

Usage notes

  • No file operands. tr a b file is an error: write tr a b < file.
  • Sets are lists of characters, not strings. tr 'cat' 'dog' turns every c into d, every a into o and every t into g; it does not replace the word. Use sed for words.
  • Short SET2: GNU tr pads SET2 with its last character (tr abcdef xy maps c to y). Use -t to truncate SET1 instead. Other implementations behave differently, so avoid relying on it.
  • Delete needs one set, -d with SET2 is an error unless combined with -s.
  • -c with newline: tr -cd '0-9' also deletes the final newline. Add \n to the set ('0-9\n') to keep line structure.
  • Ranges depend on the locale in some implementations; [:lower:] and [:upper:] are the portable choice. GNU tr works on bytes, so multi-byte characters such as accented letters are not translated reliably. Use sed, awk or perl for Unicode.
  • Case conversion with classes requires matching sizes: [:lower:] to [:upper:] works, but [:alpha:] to [:upper:] does not.
  • Quote the sets so the shell does not expand [a-z] as a glob or treat \n specially.
  • Speed: tr is very fast and uses no memory proportional to input, so it is a good first step in long pipelines.