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
sed Command Reference for CI Scriptssed is a stream editor for find-and-replace in CI builds. Reference for -i, -e, -E, and s///, plus the GNU-vs…
cut Command Reference for CI Scriptscut extracts columns by field or character in CI. Reference for -f, -d, and -c, plus the single-delimiter and…
sort Command Reference for CI Scriptssort orders lines of text in CI for stable diffs. Reference for -n, -u, -k, -t, and -r, plus the locale gotch…
base64 Command Reference for CI Scriptsbase64 encodes and decodes data for transporting secrets in CI. Reference for -d, -w0, and stdin usage, plus…