{
    "mode": "info",
    "parameter": "tr",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/tr/json",
    "generated": "2026-09-23T09:26:33Z",
    "sections": {
        "File: coreutils.info,  Node: tr invocation,  Next: expand invocation,  Up: Operating on characters": {
            "content": "",
            "subsections": [
                {
                    "name": "9.1 'tr': Translate, squeeze, and/or delete characters",
                    "content": "Synopsis:\n\ntr [OPTION]... STRING1 [STRING2]\n\n'tr' copies standard input to standard output, performing one of the\nfollowing operations:\n\n* translate, and optionally squeeze repeated characters in the\nresult,\n* squeeze repeated characters,\n* delete characters,\n* delete characters, then squeeze repeated characters from the\nresult.\n\nThe STRING1 and STRING2 operands define arrays of characters ARRAY1\nand ARRAY2.  By default ARRAY1 lists input characters that 'tr' operates\non, and ARRAY2 lists corresponding translations.  In some cases the\nsecond operand is omitted.\n\nThe program accepts the following options.  Also see *note Common\noptions::.  Options must precede operands.\n\n'-c'\n'-C'\n'--complement'\nInstead of ARRAY1, use its complement (all characters not specified\nby STRING1), in ascending order.  Use this option with caution in\nmultibyte locales where its meaning is not always clear or\nportable; see *note Character arrays::.\n\n'-d'\n'--delete'\nDelete characters in ARRAY1; do not translate.\n\n'-s'\n'--squeeze-repeats'\nReplace each sequence of a repeated character that is listed in the\nlast specified ARRAY, with a single occurrence of that character.\n\n'-t'\n'--truncate-set1'\nTruncate ARRAY1 to the length of ARRAY2.\n\nAn exit status of zero indicates success, and a nonzero value\nindicates failure.\n\n* Menu:\n\n* Character arrays::            Specifying arrays of characters.\n* Translating::                 Changing characters to other characters.\n* Squeezing and deleting::      Removing characters.\n"
                }
            ]
        },
        "File: coreutils.info,  Node: Character arrays,  Next: Translating,  Up: tr invocation": {
            "content": "",
            "subsections": []
        },
        "The STRING1 and STRING2 operands are not regular expressions, even": {
            "content": "though they may look similar.  Instead, they merely represent arrays of\ncharacters.  As a GNU extension to POSIX, an empty string operand\nrepresents an empty array of characters.\n\nThe interpretation of STRING1 and STRING2 depends on locale.  GNU\n'tr' fully supports only safe single-byte locales, where each possible\ninput byte represents a single character.  Unfortunately, this means GNU\n'tr' will not handle commands like 'tr o\" ??' the way you might expect,\nsince (assuming a UTF-8 encoding) this is equivalent to 'tr '\\303\\266'\n'\\305\\201'' and GNU 'tr' will simply transliterate all '\\303' bytes to\n'\\305' bytes, etc.  POSIX does not clearly specify the behavior of 'tr'\nin locales where characters are represented by byte sequences instead of\nby individual bytes, or where data might contain invalid bytes that are\nencoding errors.  To avoid problems in this area, you can run 'tr' in a\nsafe single-byte locale by using a shell command like 'LCALL=C tr'\ninstead of plain 'tr'.\n\nAlthough most characters simply represent themselves in STRING1 and\nSTRING2, the strings can contain shorthands listed below, for\nconvenience.  Some shorthands can be used only in STRING1 or STRING2, as\nnoted below.\n",
            "subsections": []
        },
        "Backslash escapes": {
            "content": "The following backslash escape sequences are recognized:\n\n'\\a'\nBell (BEL, Control-G).\n'\\b'\nBackspace (BS, Control-H).\n'\\f'\nForm feed (FF, Control-L).\n'\\n'\nNewline (LF, Control-J).\n'\\r'\nCarriage return (CR, Control-M).\n'\\t'\nTab (HT, Control-I).\n'\\v'\nVertical tab (VT, Control-K).\n'\\OOO'\nThe eight-bit byte with the value given by OOO, which is the\nlongest sequence of one to three octal digits following the\nbackslash.  For portability, OOO should represent a value that\nfits in eight bits.  As a GNU extension to POSIX, if the value\nwould not fit, then only the first two digits of OOO are used,\ne.g., '\\400' is equivalent to '\\0400' and represents a\ntwo-byte sequence.\n'\\\\'\nA backslash.\n\nIt is an error if no character follows an unescaped backslash.  As\na GNU extension, a backslash followed by a character not listed\nabove is interpreted as that character, removing any special\nsignificance; this can be used to escape the characters '[' and '-'\nwhen they would otherwise be special.\n",
            "subsections": []
        },
        "Ranges": {
            "content": "The notation 'M-N' expands to the characters from M through N, in\nascending order.  M should not collate after N; if it does, an\nerror results.  As an example, '0-9' is the same as '0123456789'.\n\nGNU 'tr' does not support the System V syntax that uses square\nbrackets to enclose ranges.  Translations specified in that format\nsometimes work as expected, since the brackets are often\ntransliterated to themselves.  However, they should be avoided\nbecause they sometimes behave unexpectedly.  For example, 'tr -d\n'[0-9]'' deletes brackets as well as digits.\n\nMany historically common and even accepted uses of ranges are not\nfully portable.  For example, on EBCDIC hosts using the 'A-Z' range\nwill not do what most would expect because 'A' through 'Z' are not\ncontiguous as they are in ASCII.  One way to work around this is to\nuse character classes (see below).  Otherwise, it is most portable\n(and most ugly) to enumerate the members of the ranges.\n",
            "subsections": []
        },
        "Repeated characters": {
            "content": "The notation '[C*N]' in STRING2 expands to N copies of character C.\nThus, '[y*6]' is the same as 'yyyyyy'.  The notation '[C*]' in\nSTRING2 expands to as many copies of C as are needed to make ARRAY2\nas long as ARRAY1.  If N begins with '0', it is interpreted in\noctal, otherwise in decimal.  A zero-valued N is treated as if it\nwere absent.\n",
            "subsections": []
        },
        "Character classes": {
            "content": "The notation '[:CLASS:]' expands to all characters in the\n(predefined) class CLASS.  When the '--delete' ('-d') and\n'--squeeze-repeats' ('-s') options are both given, any character\nclass can be used in STRING2.  Otherwise, only the character\nclasses 'lower' and 'upper' are accepted in STRING2, and then only\nif the corresponding character class ('upper' and 'lower',\nrespectively) is specified in the same relative position in\nSTRING1.  Doing this specifies case conversion.  Except for case\nconversion, a class's characters appear in no particular order.\nThe class names are given below; an error results when an invalid\nclass name is given.\n\n'alnum'\nLetters and digits.\n'alpha'\nLetters.\n'blank'\nHorizontal whitespace.\n'cntrl'\nControl characters.\n'digit'\nDigits.\n'graph'\nPrintable characters, not including space.\n'lower'\nLowercase letters.\n'print'\nPrintable characters, including space.\n'punct'\nPunctuation characters.\n'space'\nHorizontal or vertical whitespace.\n'upper'\nUppercase letters.\n'xdigit'\nHexadecimal digits.\n",
            "subsections": []
        },
        "Equivalence classes": {
            "content": "The syntax '[=C=]' expands to all characters equivalent to C, in no\nparticular order.  These equivalence classes are allowed in STRING2\nonly when '--delete' ('-d') and '--squeeze-repeats' '-s' are both\ngiven.\n\nAlthough equivalence classes are intended to support non-English\nalphabets, there seems to be no standard way to define them or\ndetermine their contents.  Therefore, they are not fully\nimplemented in GNU 'tr'; each character's equivalence class\nconsists only of that character, which is of no particular use.\n",
            "subsections": []
        },
        "File: coreutils.info,  Node: Translating,  Next: Squeezing and deleting,  Prev: Character arrays,  Up: tr invocation": {
            "content": "'tr' performs translation when STRING1 and STRING2 are both given and\nthe '--delete' ('-d') option is not given.  'tr' translates each\ncharacter of its input that is in ARRAY1 to the corresponding character\nin ARRAY2.  Characters not in ARRAY1 are passed through unchanged.\n\nAs a GNU extension to POSIX, when a character appears more than once\nin ARRAY1, only the final instance is used.  For example, these two\ncommands are equivalent:\n\ntr aaa xyz\ntr a z\n\nA common use of 'tr' is to convert lowercase characters to uppercase.\nThis can be done in many ways.  Here are three of them:\n\ntr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ\ntr a-z A-Z\ntr '[:lower:]' '[:upper:]'\n\nHowever, ranges like 'a-z' are not portable outside the C locale.\n\nWhen 'tr' is performing translation, ARRAY1 and ARRAY2 typically have\nthe same length.  If ARRAY1 is shorter than ARRAY2, the extra characters\nat the end of ARRAY2 are ignored.\n\nOn the other hand, making ARRAY1 longer than ARRAY2 is not portable;\nPOSIX says that the result is undefined.  In this situation, BSD 'tr'\npads ARRAY2 to the length of ARRAY1 by repeating the last character of\nARRAY2 as many times as necessary.  System V 'tr' truncates ARRAY1 to\nthe length of ARRAY2.\n\nBy default, GNU 'tr' handles this case like BSD 'tr'.  When the\n'--truncate-set1' ('-t') option is given, GNU 'tr' handles this case\nlike the System V 'tr' instead.  This option is ignored for operations\nother than translation.\n\nActing like System V 'tr' in this case breaks the relatively common\nBSD idiom:\n\ntr -cs A-Za-z0-9 '\\012'\n\nbecause it converts only zero bytes (the first element in the complement\nof ARRAY1), rather than all non-alphanumerics, to newlines.\n",
            "subsections": []
        },
        "By the way, the above idiom is not portable because it uses ranges, and": {
            "content": "it assumes that the octal code for newline is 012.  Here is a better way\nto write it:\n\ntr -cs '[:alnum:]' '[\\n*]'\n",
            "subsections": []
        },
        "File: coreutils.info,  Node: Squeezing and deleting,  Prev: Translating,  Up: tr invocation": {
            "content": "",
            "subsections": []
        },
        "When given just the '--delete' ('-d') option, 'tr' removes any input": {
            "content": "characters that are in ARRAY1.\n\nWhen given just the '--squeeze-repeats' ('-s') option and not\ntranslating, 'tr' replaces each input sequence of a repeated character\nthat is in ARRAY1 with a single occurrence of that character.\n\nWhen given both '--delete' and '--squeeze-repeats', 'tr' first\nperforms any deletions using ARRAY1, then squeezes repeats from any\nremaining characters using ARRAY2.\n\nThe '--squeeze-repeats' option may also be used when translating, in\nwhich case 'tr' first performs translation, then squeezes repeats from\nany remaining characters using ARRAY2.\n\nHere are some examples to illustrate various combinations of options:\n\n* Remove all zero bytes:\n\ntr -d '\\0'\n\n* Put all words on lines by themselves.  This converts all\nnon-alphanumeric characters to newlines, then squeezes each string\nof repeated newlines into a single newline:\n\ntr -cs '[:alnum:]' '[\\n*]'\n\n* Convert each sequence of repeated newlines to a single newline.\nI.e., delete empty lines:\n\ntr -s '\\n'\n\n* Find doubled occurrences of words in a document.  For example,\npeople often write \"the the\" with the repeated words separated by a\nnewline.  The Bourne shell script below works first by converting\neach sequence of punctuation and blank characters to a single\nnewline.  That puts each \"word\" on a line by itself.  Next it maps\nall uppercase characters to lower case, and finally it runs 'uniq'\nwith the '-d' option to print out only the words that were\nrepeated.\n\n#!/bin/sh\ncat -- \"$@\" \\\n| tr -s '[:punct:][:blank:]' '[\\n*]' \\\n| tr '[:upper:]' '[:lower:]' \\\n| uniq -d\n\n* Deleting a small set of characters is usually straightforward.  For\nexample, to remove all 'a's, 'x's, and 'M's you would do this:\n\ntr -d axM\n\nHowever, when '-' is one of those characters, it can be tricky\nbecause '-' has special meanings.  Performing the same task as\nabove but also removing all '-' characters, we might try 'tr -d\n-axM', but that would fail because 'tr' would try to interpret '-a'\nas a command-line option.  Alternatively, we could try putting the\nhyphen inside the string, 'tr -d a-xM', but that wouldn't work\neither because it would make 'tr' interpret 'a-x' as the range of\ncharacters 'a'...'x' rather than the three.  One way to solve the\nproblem is to put the hyphen at the end of the list of characters:\n\ntr -d axM-\n\nOr you can use '--' to terminate option processing:\n\ntr -d -- -axM\n",
            "subsections": []
        }
    },
    "flags": [],
    "examples": [],
    "see_also": []
}