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:


(ab string with no newline)?

  • You used a and b as tr arguments, but got e in the output?
    – techraf
    Dec 11, 2016 at 11:22
  • This answer should help you: superuser.com/questions/1155362/complement-option-in-tr Dec 11, 2016 at 13:04
  • @adc ?? That is a link to this question.
    – DavidPostill
    Dec 11, 2016 at 14:19
  • 1
    I got ab as output for your code on my machine, as it should be: It replaces everything except a with b, and that includes the newline from echo. I've no idea why you got e instead.
    – dirkt
    Dec 11, 2016 at 19:06
  • @techraf Sorry I've made a typo - the output is indeed ab. Thx for Your attention.
    – Mulligan
    Dec 11, 2016 at 19:17

1 Answer 1


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

Compare with:

printf "a" | tr -c a b


printf "a\n\n" | tr -c a b

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .