tr is a command in Unix and Unix-like operating systems. It is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set.
|Developer(s)||AT&T Bell Laboratories|
|Initial release||November 1973|
|Operating system||Unix, Unix-like, OS-9, MSX-DOS|
|License||coreutils: GNU GPL v3|
The utility reads a byte stream from its standard input and writes the result to the standard output. As arguments, it takes two sets of characters (generally of the same length), and replaces occurrences of the characters in the first set with the corresponding elements from the second set. For example,
tr 'abcd' 'jkmn'
maps all characters a to j, b to k, c to m, and d to n.
The character set may be abbreviated by using character ranges. The previous example could be written:
tr 'a-d' 'jkmn'
In POSIX-compliant versions of
tr, the set represented by a character range depends on the locale's collating order, so it is safer to avoid character ranges in scripts that might be executed in a locale different from that in which they were written. Ranges can often be replaced with POSIX character sets such as [:alpha:].
s flag causes
tr to compress sequences of identical adjacent characters in its output to a single token. For example,
tr -s '\n'
replaces sequences of one or more newline characters with a single newline.
d flag causes
tr to delete all tokens of the specified set of characters from its input. In this case, only a single character set argument is used. The following command removes carriage return characters.
tr -d '\r'
c flag indicates the complement of the first set of characters. The invocation
tr -cd '[:alnum:]'
therefore removes all non-alphanumeric characters.
The command is also available in the OS-9 shell.
Most versions of
tr, including GNU
tr and classic Unix
tr, operate on single-byte characters and are not Unicode compliant. An exception is the Heirloom Toolchest implementation, which provides basic Unicode support.
- Paul S. Dayan (1992). The OS-9 Guru - 1 : The Facts. Galactic Industrial Limited. ISBN 0-9519228-0-7.
- MSX-DOS2 Tools User's Manual by ASCII Corporation
- "tr (String) - APIdock". APIdock. Retrieved 12 August 2015.
- "tr - perldoc.perl.org". perldoc.perl.org. Retrieved 12 August 2015.
- "Tcl Built-In Commands - string manual page". Retrieved 12 August 2015.
tr– Commands & Utilities Reference, The Single UNIX Specification, Issue 7 from The Open Group
- tr(1) – Unix 8th Edition manual page.
tr(1)– FreeBSD General Commands Manual
tr(1)– Solaris 10 User Commands Reference Manual
tr(1)– Linux User's Manual – User Commands
- usage examples at examplenow.com