tr -d: Delete Characters
tr -d removes every occurrence of the characters in the set as data passes through.
tr -d is the go-to for stripping unwanted bytes: newlines, carriage returns, or whitespace from a value.
What it does
tr -d SET deletes all characters that appear in SET, passing everything else through unchanged. It is byte-oriented and streaming, which makes it ideal for cleaning command output, for example removing the trailing newline from a captured value.
Common usage
tr -d '\n' < file.txt # remove all newlines
tr -d '\r' < dos.txt > unix.txt # strip carriage returns (CRLF -> LF)
echo "a1b2c3" | tr -d '[:digit:]' # drop digits -> abc
openssl rand -base64 24 | tr -d '\n' # single-line tokenOptions
| Flag | What it does |
|---|---|
| -d | Delete characters in SET |
| [:space:] | Class for all whitespace |
| [:digit:] | Class for digits 0-9 |
| -c | Complement: delete everything NOT in SET (with -d) |
In CI
Capturing a command into a variable often leaves a trailing newline; pipe through tr -d '\n' to get a clean single-line value for an env var or header. tr -d '\r' fixes Windows line endings before tools that choke on CR.
Common errors in CI
tr -dc '[:print:]' keeps only the listed set (the -c complement deletes everything else), which is easy to get backwards. Deleting '\r' fixes CRLF files, but if you also need to add a final newline, tr -d will not; combine with printf. tr only reads stdin, so pass input via < or a pipe, not as an argument.