Skip to content
Latchkey

rev: Reverse Characters in Each Line

rev reverses the order of characters within each line, the opposite of tac which reverses lines.

rev flips each line left to right. Its main CI use is grabbing fields from the end of a string with cut.

What it does

rev reverses the characters on each line independently, leaving line order unchanged (that is tac's job). The classic trick is rev | cut -d. -f1 | rev to extract the last delimited field, since cut cannot count fields from the end.

Common usage

Terminal
echo "a.b.c" | rev | cut -d. -f1 | rev     # last field -> c
echo "path/to/file.txt" | rev | cut -d/ -f1 | rev   # basename
printf 'hello\n' | rev                      # olleh

Options

ItemWhat it does
(default)Reverse characters in each line
file ...Reverse each line of the given files
(stdin)Reverses piped input line by line

In CI

rev | cut | rev is a portable way to take the last field of a delimited string when you do not want to invoke awk. For paths specifically, basename or ${var##*/} is clearer, but rev works on any delimiter.

Common errors in CI

rev ships with util-linux on Linux and exists on macOS too, but behavior on multibyte UTF-8 differs: some builds reverse by byte and corrupt multibyte characters. For ASCII delimiters and values it is safe. CRLF endings leave the carriage return at the start after reversal; strip \r with tr -d first.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →