I was playing around with tr
and got some unexpected results. What is happening in these situations? I don't understand what is happening under the hood, or perhaps I'm not using this command correctly.
Example A
echo '0123456789' | tr [:digit:] '12'
1222222222
Example B1
echo '1111111111' | tr [:digit:] '12'
2222222222
Example B2
echo '1111111111' | tr '1' '12'
1111111111
Example C1
Works as expected.
echo '0123456789' | tr '5' 'x'
01234x6789
Example C2
I expected this to produce 01234xx6789
maybe, or it somehow explains all these examples - in that only the original character can be replaced (additional characters cannot be added).
echo '0123456789' | tr '5' 'xx'
01234x6789
[:digit:]
pattern unquoted, so the shell will replace it with any matching filename before callingtr
. – Kusalananda Nov 22 '23 at 15:59'[:digit:]'
if I wanted to (escaping the first[
seems to do the trick). – BadHorsie Nov 22 '23 at 16:02