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)?
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
acharacter 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[~]#