Skip to content
Latchkey

tr Command Reference for CI Scripts

tr maps or removes individual characters as text streams through it.

tr is the simplest character-level transformer: uppercase a string, strip carriage returns, squeeze whitespace. It only reads stdin and does not take a filename.

Common flags/usage

  • -d: delete characters in set1
  • -s: squeeze repeats of set1 into one
  • -c / -C: complement set1 (everything not in it)
  • [:class:]: character classes like [:alnum:], [:space:]
  • a-z: character ranges

Example

shell
BRANCH=$(echo "${GITHUB_REF_NAME}" | tr '/' '-')   # slug for a tag
tr -d '\r' < dos.txt > unix.txt                     # strip CRLF
echo "${RAW}" | tr -cd '[:alnum:]-'

In CI

tr only reads stdin, so tr ... file.txt treats file.txt as a second set, not a file; redirect with < file or pipe in. A reverse-collating-order error on a range like a-Z comes from a locale-confused range; use LC_ALL=C or explicit [:upper:]/[:lower:] classes. Stripping \r fixes CRLF parsing bugs.

Key takeaways

  • tr reads only stdin; redirect a file in with < instead of naming it.
  • tr -d "\r" converts CRLF files so downstream parsing does not choke.
  • Use [:upper:]/[:lower:] classes or LC_ALL=C to avoid locale range errors.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →