TR(1P)
๐ TR(1P) โ POSIX Programmer's Manual ๐
okay bestie, welcome to the tr page, we're doing this in parts and part one is right here, so get comfy ๐ธ
โจ PROLOG โจ
so like, this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ โ which means you gotta consult the corresponding Linux manual page for details of Linux behavior ๐ซถ โ or honestly, the interface might just not be implemented on Linux at all. no cap. ๐
๐ NAME ๐
tr โ translate characters โจ like that's literally her whole job, translating characters, slay
๐ฆ SYNOPSIS ๐ฆ
tr [-c|-C] [-s] string1 string2
tr -s [-c|-C] string1
tr -d [-c|-C] string1
tr -ds [-c|-C] string1 string2
four whole ways to call her, pick your fighter ๐
๐ DESCRIPTION ๐
okay so the tr utility shall copy the standard input to the standard output, but with substitution or deletion of selected characters. the way that it works is: the options you specify plus the string1 and string2 operands shall control translations that occur while copying characters and single-character collating elements. yes, collating elements, she's fancy like that ๐
๐ OPTIONS ๐
the tr utility shall conform to the Base Definitions volume of POSIX.1โ2017, Section 12.2, Utility Syntax Guidelines. iconic behavior, we love a utility that follows the rules ๐
the following options shall be supported:
-cโ Complement the set of values specified bystring1. See the EXTENDED DESCRIPTION section. (complement the values, keep that in your notes bestie ๐)-Cโ Complement the set of characters specified bystring1. See the EXTENDED DESCRIPTION section. (capital C, complements characters โ yes these two are lowkey different, don't @ me, it's in the EXTENDED DESCRIPTION)-dโ Delete all occurrences of input characters that are specified bystring1. bye bye characters, gone, deleted, periodt ๐-sโ Replace instances of repeated characters with a single character, as described in the EXTENDED DESCRIPTION section. squish them together, one character only, no duplicates allowed ๐ซถ
๐ธ OPERANDS ๐ธ
the following operands shall be supported:
string1,string2โ Translation control strings. Each string shall represent a set of characters to be converted into an array of characters used for the translation. For a detailed description of how the strings are interpreted, see the EXTENDED DESCRIPTION section. (yes bestie, the details are further down, we're getting there, patience is a virtue ๐ )
โญ STDIN โญ
The standard input can be any type of file. literally any type. she's not picky, she's versatile, main character energy ๐ฅ
๐ง INPUT FILES ๐ง
None. nothing. nada. empty. zero files, she doesn't need them ๐
๐ ENVIRONMENT VARIABLES ๐
the following environment variables shall affect the execution of tr:
LANGโ Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1โ2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)LC_ALLโ If set to a non-empty string value, override the values of all the other internationalization variables. she said ALL. she's the boss of the other variables, respect her ๐LC_COLLATEโ Determine the locale for the behavior of range expressions and equivalence classes.LC_CTYPEโ Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments) and the behavior of character classes. this one is doing a LOT, highkey the busiest variable here ๐ญLC_MESSAGESโ Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.NLSPATHโ Determine the location of message catalogs for the processing ofLC_MESSAGES. she's the map, she finds the message catalogs ๐บ๏ธโจ
๐ซ ASYNCHRONOUS EVENTS ๐ซ
Default. nothing special, just vibing ๐ฆ
โจ STDOUT โจ
the tr output shall be identical to the input, with the exception of the specified transformations. same energy, just with the glow-up applied ๐
๐ฉท STDERR ๐ฉท
The standard error shall be used only for diagnostic messages. only for the drama, never for the actual output ๐
๐ OUTPUT FILES ๐
None. again. zero. we don't do files here ๐ง
โจ EXTENDED DESCRIPTION โจ
okay bestie, this is where tr stops playing and starts being that girl. so string1 and string2 (if you even bothered to give her one) define two arrays of characters. and there's a whole lil menu of constructs you can use to specify characters or single-character collating elements. heads up tho: if any of those constructs end up producing multi-character collating elements, tr shall exclude those multi-character elements from the resulting array โ no diagnostic, no explanation, they just get ghosted. ๐
characterโ any character that isn't described by one of the conventions below? it represents itself. that's it, that's the whole job. no notes. ๐\octalโ octal sequences are how you represent characters with specific coded values. an octal sequence is a<backslash>followed by the longest sequence of one, two, or three octal-digit characters (01234567). that sequence shall cause the value whose encoding is represented by the one, two, or three-digit octal integer to be placed into the array. multi-byte characters? you're gonna need multiple, concatenated escape sequences of this type, including the leading<backslash>for each byte. every byte carries its own backslash, periodt.\characterโ the<backslash>-escape sequences in the Base Definitions volume of POSIX.1โ2017, Table 5-1, Escape Sequences and Associated Actions ('\\','\a','\b','\f','\n','\r','\t','\v') shall be supported. using any other character after the<backslash>โ anything that isn't an octal digit โ is unspecified. and if there's literally no character following the<backslash>at all? also unspecified. so don't. ๐ญc-cโ in the POSIX locale, this construct shall represent the range of collating elements between the range endpoints, inclusive, as defined by the collation sequence โ as long as neither endpoint is an octal sequence of the form\octal. the characters or collating elements in that range get placed in the array in ascending collation sequence. now, if the second endpoint comes before the starting endpoint in the collation sequence, it is unspecified whether the range of collating elements is empty, or whether this construct is just treated as invalid. ๐ in locales other than the POSIX locale, this construct has unspecified behavior. If either or both of the range endpoints are octal sequences of the form\octal, then this represents the range of specific coded values between the two range endpoints, inclusive.[:class:]โ represents all characters belonging to the defined character class, as defined by the current setting of theLC_CTYPElocale category. these are the class names that shall be accepted when specified instring1:alnum,blank,digit,lower,punct,upperalpha,cntrl,graph,print,space,xdigitIn addition, character class expressions of the form[:name:]shall be recognized in those locales where thenamekeyword has been given acharclassdefinition in theLC_CTYPEcategory. When both the-dand-soptions are specified, any of the character class names shall be accepted instring2. otherwise? only the character class nameslowerorupperare valid instring2, and then only if the corresponding character class (upperandlower, respectively) is specified in the same relative position instring1. a spec like that shall be interpreted as a request for case conversion. when[:lower:]appears instring1and[:upper:]appears instring2, the arrays shall contain the characters from thetouppermapping in theLC_CTYPEcategory of the current locale. when[:upper:]appears instring1and[:lower:]appears instring2, the arrays shall contain the characters from thetolowermapping in theLC_CTYPEcategory of the current locale. the first character from each mapping pair shall be in the array forstring1, and the second character from each mapping pair shall be in the array forstring2, in the same relative position. slay. ๐ Except for case conversion, the characters specified by a character class expression shall be placed in the array in an unspecified order. and if the name specified forclassdoesn't define a valid character class in the current locale, the behavior is undefined. beware. โ ๏ธ
[=equiv=]โ represents all characters or collating elements belonging to the same equivalence class asequiv, as defined by the current setting of theLC_COLLATElocale category. an equivalence class expression shall be allowed only instring1, or instring2when it's being used by the combined-dand-soptions. the characters belonging to the equivalence class shall be placed in the array in an unspecified order.[x*n]โ representsnrepeated occurrences of the characterx. because this expression is used to map multiple characters to one, it is only valid when it occurs instring2. ifnis omitted or is zero, it shall be interpreted as large enough to extend thestring2-based sequence to the length of thestring1-based sequence. ifnhas a leading zero, it shall be interpreted as an octal value. otherwise, it shall be interpreted as a decimal value.
When the -d option is not specified:
- If
string2is present, each input character found in the array specified bystring1shall be replaced by the character in the same relative position in the array specified bystring2. if the array specified bystring2is shorter than the one specified bystring1, or if a character occurs more than once instring1, the results are unspecified. ๐ซฃ - If the
-Coption is specified, the complements of the characters specified bystring1โ meaning the set of all characters in the current character set, as defined by the current setting ofLC_CTYPE, except for those actually specified in thestring1operand โ shall be placed in the array in ascending collation sequence, as defined by the current setting ofLC_COLLATE. - If the
-coption is specified, the complement of the values specified bystring1shall be placed in the array in ascending order by binary value. - Because the order in which characters specified by character class expressions or equivalence class expressions is undefined, such expressions should only be used if the intent is to map several characters into one. the exception is case conversion, as described previously. โจ
When the -d option is specified:
- Input characters found in the array specified by
string1shall be deleted. bye bestie. ๐ - When the
-Coption is specified with-d, all characters except those specified bystring1shall be deleted. the contents ofstring2are ignored, unless the-soption is also specified. - When the
-coption is specified with-d, all values except those specified bystring1shall be deleted. the contents ofstring2shall be ignored, unless the-soption is also specified. - The same string cannot be used for both the
-dand the-soption; when both options are specified, bothstring1(used for deletion) andstring2(used for squeezing) shall be required. no shortcuts, sorry. ๐
When the -s option is specified, after any deletions or translations have taken place, repeated sequences of the same character shall be replaced by one occurrence of the same character, if the character is found in the array specified by the last operand. if the last operand contains a character class, like this lil example right here:
tr -s '[:space:]'
then the last operand's array shall contain all of the characters in that character class. however, in a case conversion, as described previously, like this:
tr -s '[:upper:]' '[:lower:]'
the last operand's array shall contain only those characters defined as the second characters in each of the toupper or tolower character pairs, as appropriate.
An empty string used forstring1orstring2produces undefined results. so like, don't. ๐ญ
๐ EXIT STATUS ๐
here's the tea on exit values:
0โ all input was processed successfully. we ate. ๐>0โ an error occurred. it's giving rough. ๐
๐ CONSEQUENCES OF ERRORS ๐
Default.
the following sections are informative.
๐ APPLICATION USAGE ๐
- If necessary,
string1andstring2can be quoted to avoid pattern matching by the shell. protect your babies. ๐ซถ - If an ordinary digit (representing itself) is to follow an octal sequence, the octal sequence must use the full three digits to avoid ambiguity. three digits or it's chaos, no cap.
- When
string2is shorter thanstring1, a difference results between historical System V and BSD systems. a BSD system padsstring2with the last character found instring2. so like, it's possible to do this:
tr 0123456789 d
which would translate all digits to the letter 'd'. since this area is specifically unspecified in this volume of POSIX.1โ2017, both the BSD and System V behaviors are allowed โ but a conforming application cannot rely on the BSD behavior. it would have to code the example in the following way instead:
tr 0123456789 '[d*]'
- It should be noted that, despite similarities in appearance, the string operands used by
trare not regular expressions. i know they look alike. they are not the same. do not confuse them. ๐ โโ๏ธ - Unlike some historical implementations, this definition of the
trutility correctly processesNULcharacters in its input stream.NULcharacters can be stripped by using:
tr -d '\000'
๐ EXAMPLES ๐
- The following example creates a list of all words in
file1, one per line, infile2, where a word is taken to be a maximal string of letters.
tr -cs "[:alpha:]" "[\n*]" <file1 >file2
- The next example translates all lowercase characters in
file1to uppercase and writes the results to standard output.
tr "[:lower:]" "[:upper:]" <file1
- This example uses an equivalence class to identify accented variants of the base character
'e'infile1, which are stripped of diacritical marks and written tofile2.
tr "[=e=]" "[e*]" <file1 >file2
๐ญ RATIONALE (the lore, bestie)
Okay so back in some early proposals, there was an explicit option -n that existed purely to turn off the historical behavior of stripping NUL characters out of the input. Like, the vibe at the time was "hmm, auto-stripping NUL characters is not correct functionality." ๐ But then -n got removed in a later proposal โ and removing it does not remove the requirement that tr correctly process NUL characters in its input stream. So if you want them gone? You do it yourself with tr -d '\000'. Iconic, honestly. ๐
Historical implementations of tr differ wildly in syntax and behavior โ not even a little, like wildly. For example, the BSD version has never needed the bracket characters for the repetition sequence. The tr utility syntax is based more closely on the System V and XPG3 model, while also trying to accommodate historical BSD implementations. In the case of the short string2 padding, the decision was to unspecify the behavior and preserve System V and XPG3 scripts, which might find difficulty with the BSD method. The assumption was made that BSD users of tr have to make accommodations to meet the syntax defined here. Since it is possible to use the repetition sequence to duplicate the desired behavior, whereas there is no simple way to achieve the System V method, this was the correct โ if not desirable โ approach. ๐ซถ
The use of octal values to specify control characters, while having historical precedents, is not portable. The introduction of escape sequences for control characters should provide the necessary portability. It is recognized that this may cause some historical scripts to break. ๐
An early proposal included support for multi-character collating elements. It was pointed out that, while tr does employ some syntactical elements from REs, the aim of tr is quite different; ranges, for example, do not have a similar meaning (any of the chars in the range matches'', versus translate each character in the range to the output counterpart''). As a result, the previously included support for multi-character collating elements has been removed. What remains are ranges in current collation order (to support, for example, accented characters), character classes, and equivalence classes. โจ
In XPG3 the [:class:] and [=equiv=] conventions are shown with double brackets, as in RE syntax. However, tr does not implement RE principles; it just borrows part of the syntax. Consequently, [:class:] and [=equiv=] should be regarded as syntactical elements on a par with [x*n], which is not an RE bracket expression. ๐ง
The standard developers will consider changes to tr that allow it to translate characters between different character encodings, or they will consider providing a new utility to accomplish this. ๐ (someday, bestie, someday)
On historical System V systems, a range expression requires enclosing square-brackets, such as:
tr '[a-z]' '[A-Z]'
However, BSD-based systems did not require the brackets, and this convention is used here to avoid breaking large numbers of BSD scripts:
tr a-z A-Z
The preceding System V script will continue to work because the brackets, treated as regular characters, are translated to themselves. However, any System V script that relied on "aโz" representing the three characters 'a', '-', and 'z' have to be rewritten as "az-". ๐
The ISO POSIXโ2:1993 standard had a -c option that behaved similarly to the -C option, but did not supply functionality equivalent to the -c option specified in POSIX.1โ2008. (confusing? yes. our problem now? no ๐
)
The earlier version also said that octal sequences referred to collating elements and could be placed adjacent to each other to specify multi-byte characters. However, it was noted that this caused ambiguities, because tr would not be able to tell whether adjacent octal sequences were intending to specify multi-byte characters or multiple single byte characters. POSIX.1โ2008 specifies that octal sequences always refer to single byte binary values when used to specify an endpoint of a range of collating elements. ๐
Earlier versions of this standard allowed for implementations with bytes other than eight bits, but this has been modified in this version. โ๏ธ
๐ฎ FUTURE DIRECTIONS
None. ๐
That's it. That's the whole section. Nothing coming. No cap.
๐ SEE ALSO
sed
The Base Definitions volume of POSIX.1โ2017, Table 5-1, Escape Sequences and Associated Actions, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines
ยฉ๏ธ COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
And that's a wrap on TR(1P), bestie!! ๐ This whole page came to you courtesy of IEEE/The Open Group, dated 2017 โ so yeah, it's the POSIX girlies' edition, and she ate. ๐ Go forth and translate, delete, and squeeze those chars. You got this. ๐๐ฆโจ
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms