{{short description|Unix text formatting utility}} {{lowercase title}} {{Infobox software | name = tr | logo = | screenshot = Tr Unix.png | screenshot size = | caption = The {{code|tr}} command | author = Douglas McIlroy<br />(AT&T Bell Laboratories) | developer = Various open-source and commercial developers | released = {{Start date and age|1973|11}} | latest release version = | latest release date = | programming language = C | operating system = Unix, Unix-like, Plan 9, Inferno, OS-9, MSX-DOS, IBM i | platform = Cross-platform | genre = Command | license = coreutils: GPLv3+<br />Plan 9: MIT License | website = }} '''tr''' is a command in Unix, Plan 9, Inferno, 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.
==Overview== 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 <code>tr</code>, 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:]''.
The ''<code>s</code>'' flag causes <code>tr</code> 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.
The ''<code>d</code>'' flag causes <code>tr</code> 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'
The ''<code>c</code>'' flag indicates the complement of the first set of characters. The invocation
tr -cd '[:alnum:]'
therefore removes all non-alphanumeric characters.
==Implementations== The original version of <code>tr</code> was written by Douglas McIlroy and was introduced in Version 4 Unix.<ref name="reader">{{cite tech report |first1=M. D. |last1=McIlroy |author-link1=Doug McIlroy |year=1987 |url=http://www.cs.dartmouth.edu/~doug/reader.pdf |title=A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 |series=Computing Science |number=139 |institution=AT&T Bell Laboratories |df=dmy-all }}</ref>
The version of <code>tr</code> bundled in GNU coreutils was written by Jim Meyering.<ref>{{Cite web|url=https://linux.die.net/man/1/tr|title=Tr(1): Translate/Delete char - Linux man page}}</ref> The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities.<ref>{{Cite web|url=https://unxutils.sourceforge.net/|title=Native Win32 ports of some GNU utilities|website=unxutils.sourceforge.net|access-date=2025-08-11|archive-date=2006-02-09|archive-url=https://web.archive.org/web/20060209022842/http://unxutils.sourceforge.net/|url-status=live}}</ref> It is also available in the OS-9 shell.<ref>{{cite book|author=Paul S. Dayan|year=1992|title=The OS-9 Guru - 1 : The Facts|publisher=Galactic Industrial Limited|isbn=0-9519228-0-7}}</ref> A <code>tr</code> command is also part of ASCII's ''MSX-DOS2 Tools'' for MSX-DOS version 2.<ref>[https://archive.org/details/MSXDOS2TOOLS MSX-DOS2 Tools User's Manual by ASCII Corporation]</ref> The {{Mono|tr}} command has also been ported to the IBM i operating system.<ref>{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |website=IBM |author-link=IBM |url=https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |access-date=2020-09-05 |archive-date=2020-09-18 |archive-url=https://web.archive.org/web/20200918130823/https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |url-status=live }}</ref>
Most versions of <code>tr</code>, including GNU <code>tr</code> and classic Unix <code>tr</code>, operate on single-byte characters and are not Unicode compliant. An exception is the Heirloom Toolchest implementation, which provides basic Unicode support.
Ruby and Perl also have an internal ''tr'' operator, which operates analogously.<ref>{{cite web|title=tr (String) - APIdock|url=https://apidock.com/ruby/String/tr|publisher=APIdock|access-date=12 August 2015}}</ref><ref name=Perl_trDoc>{{cite web|title=tr - perldoc.perl.org|url=https://perldoc.perl.org/functions/tr.html|publisher=perldoc.perl.org|access-date=12 August 2015}}</ref> Tcl's ''string map'' command is more general in that it maps strings to strings while tr maps characters to characters.<ref name=TCLManualPage>{{cite web|title=Tcl Built-In Commands - string manual page|url=https://www.tcl.tk/man/tcl8.4/TclCmd/string.htm#M33|access-date=12 August 2015|archive-date=15 April 2018|archive-url=https://web.archive.org/web/20180415001909/http://www.tcl.tk/man/tcl8.4/TclCmd/string.htm#M33|url-status=live}}</ref>
==See also== * sed * List of Unix commands * GNU Core Utilities
==References== {{Reflist}}
==External links== {{Wikibooks|Guide to Unix|Commands}} * {{man|cu|tr|SUS}} * [http://man.cat-v.org/unix_8th/1/tr tr(1)] – Unix 8th Edition manual page. * {{man|1|tr|FreeBSD}} * {{man|1|tr|Solaris}} * {{man|1|tr|Linux}} * {{man|1|tr|Plan 9}} * {{man|1|tr|Inferno}} * [https://web.archive.org/web/20120723113341/http://www.examplenow.com/tr/ usage examples at examplenow.com]
{{Unix commands}} {{Plan 9 commands}} {{Core Utilities commands}}
Category:Unix text processing utilities Category:Unix SUS2008 utilities Category:Plan 9 commands Category:Inferno (operating system) commands Category:IBM i Qshell commands