Celeb Glow
updates | March 10, 2026

Complement option in tr

I can't understand the complement (-c) option in tr command used along with the replace mode (that is without any other options), for e.g:

echo "a" | tr -c a b

Why does it produce:

abroot@Slack

(ab string with no newline)?

6

1 Answer

tr command processes all characters, including the non-printing ones.

echo in your example produces an output consisting of two characters:

  • a
  • newline character (\n)

In your call you ordered tr to replace all characters which complement a (in simple words: other than a) with b, so:

  • it left the a character intact
  • replaced the newline character with b.

Consider testing it with printf (which does not implicitly add a newline to the end, like echo does)

This produces the same input for tr as echo, so the output is also the same:

printf "a\n" | tr -c a b
ab[~]#

Compare with:

printf "a" | tr -c a b
a[~]#

And:

printf "a\n\n" | tr -c a b
abb[~]#

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy