Skip to main content

Command referencetr

tr command (Linux / bash / sh / zsh)

Linuxbashshzshtextbeginner

What it does

Translate, squeeze, or delete characters in a byte stream. When people search for the tr command linux characters, they usually want tr's everyday flags, safe examples, and how it differs from related tools. Below you'll find syntax, options, and copyable examples for Bash/sh/zsh.

Translate, squeeze, or delete characters in a byte stream.

Syntax

tr [OPTION]... SET1 [SET2]

Common options

Flag / optionMeaning
-dDelete characters from SET1.
-sSqueeze repeated characters.
-cUse the complement of SET1.
-tTruncate SET1 to SET2 length.

Practical examples

Uppercase text

printf '%s
' hello | tr '[:lower:]' '[:upper:]'

Converts lowercase letters to uppercase.

Remove carriage returns

tr -d '\r' < windows.txt > unix.txt

Removes CR bytes from line endings.

Squeeze spaces

tr -s ' ' < report.txt

Collapses runs of spaces.

Common mistakes

  • Expecting tr to replace multicharacter strings; it operates on characters.
  • Relying on locale-sensitive character classes without controlling locale.

FAQ

What does tr do in Linux?

Translate, squeeze, or delete characters in a byte stream.

How do I use tr from the terminal?

Start with `tr [OPTION]... SET1 [SET2]`, then choose only the options needed for the task.

What is a practical tr example?

Try `printf '%s ' hello | tr '[:lower:]' '[:upper:]'` — Converts lowercase letters to uppercase.

Can I use tr in a shell script?

Yes. tr works in Bash, sh, and Zsh, but scripts should handle quoting, errors, and command output explicitly.

Browse the full Linux and Windows command reference to search more bash, CMD, PowerShell, Conda, and GitHub CLI commands.