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)?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communitytr
command processes all characters, including the non-printing ones.
echo
in your example produces an output consisting of two characters:
a
\n
)In your call you ordered tr
to replace all characters which complement a
(in simple words: other than a
) with b
, so:
a
character intactb
.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[~]#
a
andb
astr
arguments, but gote
in the output?ab
as output for your code on my machine, as it should be: It replaces everything excepta
withb
, and that includes the newline from echo. I've no idea why you gote
instead.