Back to the command reference

tr

ShellFundamentals In simulator In real Linux

Translate or delete characters

Name: tr = translate

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

tr reads standard input and replaces each character of set 1 with the matching character of set 2; -d deletes characters and -s squeezes repeats into one.

tr only reads standard input and takes no file names, so you write cat file | tr ... or tr ... < file.

Syntax

tr [OPTION]... SET1 [SET2]

Common options

a-zA character range; classes like [:upper:] [:digit:] work too
[:lower:] / [:upper:] / [:digit:] / [:space:]Character classes: lower case, upper case, digits, white space, e.g. tr '[:lower:]' '[:upper:]'
-d / --deleteDelete characters in SET1
-s / --squeeze-repeatsSqueeze repeated characters into one
-c / --complementUse the characters not in the set, e.g. tr -cd '0-9' keeps only digits

Examples

  • $ echo hello | tr a-z A-Z

    Convert to upper case

    Explain
  • $ cat README.md | tr -d "#"

    Delete every #

    Explain
  • $ echo "a,b,c" | tr , "\n"

    Turn commas into newlines

    Explain

Related