tr command (Linux / bash / sh / zsh)
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 / option | Meaning |
|---|---|
| -d | Delete characters from SET1. |
| -s | Squeeze repeated characters. |
| -c | Use the complement of SET1. |
| -t | Truncate 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.
Related commands
- sedTransform text streams with scripted substitutions and edits.
- cutExtract selected fields, characters, or bytes from each input line.
- catConcatenate files and write their contents to standard output.
- grepSearch text for lines matching a pattern.
- Where-ObjectFilter pipeline objects by property values or a script block predicate.
- Get-ContentRead file content or provider data as a stream of objects.
- echoWrite arguments and shell-expanded values to standard output.
- echoDisplay text or control command echoing in a batch script.
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.