What tr does
tr reads standard input and writes standard output. It takes a set of characters called SET1 and, depending on the mode, maps them to SET2, deletes them, or squeezes repeats. It does not take file names as arguments, so you always feed it through a pipe or a redirection. It does not know about lines either. It sees one character at a time, and a newline is just another character.
The mapping is positional. The first character of SET1 becomes the first character of SET2, and so on. If SET2 is shorter, its last character is repeated to fill the gap.
Translating characters
The basic form is tr SET1 SET2. Ranges such as a-z expand to the characters between the endpoints. The common use is changing case.
echo 'ello, World'| tr '-z''-Z'The output is HELLO, WORLD. Punctuation and spaces are not in the range, so they pass through unchanged. Translating one character to another is also useful for swapping separators. For example, tr ':' ' ' turns colons into spaces.
Use the named classes [:lower:] and [:upper:] when you want the letters of the current locale. A plain range such as a-z may behave differently in some locales, so the class form is the portable choice.
Character classes
A named class is written inside SET1 or SET2 as [:name:]. Common classes are [:alpha:] for letters, [:digit:] for digits, [:alnum:] for both, [:space:] for whitespace, [:punct:] for punctuation, and [:upper:] and [:lower:] for case. The brackets around the class are part of the class syntax, so in a shell command you still quote the whole set.
echo 'rder 66, ship 12!'| tr -cd ':digit:]'The output is 6612. The -c option complements the set, so SET1 becomes every character that is not a digit, and -d deletes those characters. The result is the digits only, with no newline at the end because the input newline was also deleted. This complement-and-delete pattern is one of the most useful tr idioms.
Deleting and squeezing
-d deletes every character in SET1. -s squeezes runs of the same character in SET1 down to one. Squeezing is how you turn a line with irregular spacing into a single-space separated line, which makes cut and awk easier to use.
echo ' b c'| tr -s ''The output is a b c. Only the repeated spaces are reduced, and every other character is unchanged. Use [:space:] instead of a literal space when the input may contain tabs as well.
Working with newlines
tr is the standard way to turn a stream of words into one word per line, or the reverse. It is the step that the ranking pipeline in the sort unit relied on. Because tr sees the newline as a character, tr '\n' ' ' joins lines into a single line separated by spaces.
printf 'ne\ntwo\nthree\n'| tr '
''' echoThe output is one two three, followed by the newline from the separate echo command. The trailing newline of the original input became a space, so the line has no newline of its own until echo adds one.
Translating with a complement
The complement flag also works with translation. With -c and a SET2, every character outside SET1 is mapped to SET2. This is handy for replacing everything that is not a letter or digit with a single marker, such as an underscore, so that a messy label becomes a safe identifier.
echo 'y file (v2).txt'| tr -c ':alnum:]\n'''The output is my_file__v2__txt. Every space, parenthesis, and dot became an underscore, and the letters and digits were left alone. The newline at the end of the input is in the set, so it is kept as well. Because the mapping is character by character, it is quick, but it cannot tell a dot that starts a file extension from a dot in the middle of a name.
Combine the flags carefully. Using -s with the same set squeezes the underscores that the translation produced, so my file name with two spaces becomes a single underscore rather than two. This pairing of translate and squeeze is a standard cleanup step before text is used in a file name or a database key.
Because tr works on one character at a time, it is also very cheap. It keeps no state between characters beyond the translation table it builds at startup, so it can process large streams with little overhead. That is one reason it appears in many shell pipelines, often right after a command that prints wide or messy text. A common order is to squeeze spaces with tr first, then let cut or awk split the fields, because the squeezed text has one predictable delimiter.
What tr cannot do
tr works on single characters and cannot match a sequence. It cannot replace the word cat with dog, because that requires a multi-character match, and tr would map the letters one by one. For that job use sed. The rule of thumb is simple: use tr when every change is a character-for-character change, and use sed or awk when you match text.
echo 'at'| tr 'at''og'This prints dog, but only by accident of the positions. The tr command translated c to d, a to o, and t to g, one character at a time. Replacing cat wherever it appears in a longer line would need sed, which the next unit covers.