Overview
tr reads standard input and writes it back after replacing, deleting, or squeezing characters. It works on single characters, not words or patterns, and it never takes a file name.
Background
tr translates, deletes, or squeezes characters read from standard input, and it takes no file name: you always feed it with a pipe or a redirection. Neither the FreeBSD nor the macOS manual gives a history note for it, so this page states none. Today the name covers GNU tr, the tr that macOS ships, and the tr applet in BusyBox. They agree on ranges, character classes, and -c, -d, -s as shown in the examples, and differ on multibyte text and rarer features.
Examples
Change lowercase letters to uppercase:
echo 'hello' | tr 'a-z' 'A-Z'
Do the same with character classes:
echo 'hello' | tr '[:lower:]' '[:upper:]'
Squeeze repeated spaces into one:
echo 'a b c' | tr -s ' '
Delete every letter o:
echo 'hello world' | tr -d 'o'
Keep only digits by deleting everything else (the newline is deleted too):
echo 'abc123' | tr -cd '0-9'
Turn colons into newlines:
echo 'a:b:c' | tr ':' '\n'
Turn the commas of a file into tabs by redirecting it in:
tr ',' '\t' < data.csv
Options
-d
Delete the characters in the set instead of translating them.
-s
Squeeze runs of a repeated character from the last set into a single one.
-c
Use the complement of the first set, meaning every character not listed.
Common mistakes
tr reads standard input only, so use tr ... < file or a pipe, not tr ... file. Sets are characters, not words: tr 'cat' 'dog' maps each letter separately. Keep both sets the same length when translating, because behavior for unequal sets differs between implementations. Quote sets so the shell does not expand brackets or ranges.
Portability
-d, -s, and -c with ranges and the classes above are in the Common examples. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check). Multibyte-character behavior depends on the locale and implementation.
References
Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.