{
    "mode": "info",
    "parameter": "tr",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/tr/json",
    "generated": "2026-07-29T05:11:49Z",
    "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]... SET1 [SET2]\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 SET1 and (if given) SET2 arguments define ordered sets of\ncharacters, referred to below as SET1 and SET2.  These sets are the\ncharacters of the input that 'tr' operates on.  The '--complement'\n('-c', '-C') option replaces SET1 with its complement (all of the\ncharacters that are not in SET1).\n\nCurrently 'tr' fully supports only single-byte characters."
                }
            ]
        },
        "Eventually it will support multibyte characters; when it does, the '-C'": {
            "content": "option will cause it to complement the set of characters, whereas '-c'\nwill cause it to complement the set of values.  This distinction will\nmatter only when some values are not characters, and this is possible\nonly in locales using multibyte encodings when the input contains\nencoding errors.\n\nThe program accepts the '--help' and '--version' options.  *Note\nCommon options::.  Options must precede operands.\n\nAn exit status of zero indicates success, and a nonzero value\nindicates failure.\n\n* Menu:\n\n* Character sets::              Specifying sets of characters.\n* Translating::                 Changing one set of characters to another.\n* Squeezing and deleting::      Removing characters.\n",
            "subsections": []
        },
        "File: coreutils.info,  Node: Character sets,  Next: Translating,  Up: tr invocation": {
            "content": "",
            "subsections": []
        },
        "The format of the SET1 and SET2 arguments resembles the format of": {
            "content": "regular expressions; however, they are not regular expressions, only\nlists of characters.  Most characters simply represent themselves in\nthese strings, but the strings can contain the shorthands listed below,\nfor convenience.  Some of them can be used only in SET1 or SET2, as\nnoted below.\n",
            "subsections": []
        },
        "Backslash escapes": {
            "content": "The following backslash escape sequences are recognized:\n\n'\\a'\nControl-G.\n'\\b'\nControl-H.\n'\\f'\nControl-L.\n'\\n'\nControl-J.\n'\\r'\nControl-M.\n'\\t'\nControl-I.\n'\\v'\nControl-K.\n'\\OOO'\nThe 8-bit character with the value given by OOO, which is 1 to\n3 octal digits.  Note that '\\400' is interpreted as the\ntwo-byte sequence, '\\040' '0'.\n'\\\\'\nA backslash.\n\nWhile a backslash followed by a character not listed above is\ninterpreted as that character, the backslash also effectively\nremoves any special significance, so it is useful to escape '[',\n']', '*', and '-'.\n",
            "subsections": []
        },
        "Ranges": {
            "content": "The notation 'M-N' expands to all of the characters from M through\nN, in ascending order.  M should collate before N; if it doesn't,\nan error results.  As an example, '0-9' is the same as\n'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\nportable.  For example, on EBCDIC hosts using the 'A-Z' range will\nnot do what most would expect because 'A' through 'Z' are not\ncontiguous as they are in ASCII.  If you can rely on a POSIX\ncompliant version of 'tr', then the best way to work around this is\nto use character classes (see below).  Otherwise, it is most\nportable (and most ugly) to enumerate the members of the ranges.\n",
            "subsections": []
        },
        "Repeated characters": {
            "content": "The notation '[C*N]' in SET2 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 SET2\nas long as SET1.  If N begins with '0', it is interpreted in octal,\notherwise in decimal.\n",
            "subsections": []
        },
        "Character classes": {
            "content": "The notation '[:CLASS:]' expands to all of the characters in the\n(predefined) class CLASS.  The characters expand in no particular\norder, except for the 'upper' and 'lower' classes, which expand in\nascending order.  When the '--delete' ('-d') and\n'--squeeze-repeats' ('-s') options are both given, any character\nclass can be used in SET2.  Otherwise, only the character classes\n'lower' and 'upper' are accepted in SET2, and then only if the\ncorresponding character class ('upper' and 'lower', respectively)\nis specified in the same relative position in SET1.  Doing this\nspecifies case conversion.  The class names are given below; an\nerror results when an invalid class 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 of the characters that are\nequivalent to C, in no particular order.  Equivalence classes are a\nrelatively recent invention intended to support non-English\nalphabets.  But 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 sets,  Up: tr invocation": {
            "content": "'tr' performs translation when SET1 and SET2 are both given and the\n'--delete' ('-d') option is not given.  'tr' translates each character\nof its input that is in SET1 to the corresponding character in SET2.",
            "subsections": []
        },
        "Characters not in SET1 are passed through unchanged.  When a character": {
            "content": "appears more than once in SET1 and the corresponding characters in SET2\nare not all the same, only the final one is used.  For example, these\ntwo commands 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\nBut note that using ranges like 'a-z' above is not portable.\n\nWhen 'tr' is performing translation, SET1 and SET2 typically have the\nsame length.  If SET1 is shorter than SET2, the extra characters at the\nend of SET2 are ignored.\n\nOn the other hand, making SET1 longer than SET2 is not portable;\nPOSIX says that the result is undefined.  In this situation, BSD 'tr'\npads SET2 to the length of SET1 by repeating the last character of SET2\nas many times as necessary.  System V 'tr' truncates SET1 to the length\nof SET2.\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 SET1), 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.  Assuming a POSIX\ncompliant 'tr', here is a better way to 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 SET1.\n\nWhen given just the '--squeeze-repeats' ('-s') option and not\ntranslating, 'tr' replaces each input sequence of a repeated character\nthat is in SET1 with a single occurrence of that character.\n\nWhen given both '--delete' and '--squeeze-repeats', 'tr' first\nperforms any deletions using SET1, then squeezes repeats from any\nremaining characters using SET2.\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 SET2.\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 blank 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\nMore generally, use the character class notation '[=c=]' with '-'\n(or any other character) in place of the 'c':\n\ntr -d '[=-=]axM'\n\nNote how single quotes are used in the above example to protect the\nsquare brackets from interpretation by a shell.\n",
            "subsections": []
        }
    },
    "flags": [],
    "examples": [],
    "see_also": []
}