Search code examples
bashshellcommandechotr

Why does an additional `*` gets appended to the output in the following bash command?


When I run the command $ echo "Hello, World!" | tr -c 'aeiou' '*', the terminal returns *e**o***o*****. There are only 4 characters after the last vowel o, so tr should replace each of them with a * to return *e**o***o****, but it is adding one more * to the output string which seems illogical to me.

I also tried $ echo "o!" | tr -c 'aeiou' '*', but still it is returning o** instead of o*.

Can anyone please help me understand the reason?


Solution

  • echo 'o!' outputs three characters: o, ! and a Line Feed.

    $ echo 'o!' | od -t c
    0000000   o   !  \n
    0000003
    

    printf 'o!' only outputs the first two.

    $ printf 'o!' | od -t c
    0000000   o   !
    0000002
    

    Alternatively, you could preserve the Line Feed using tr -c 'aeiou\n' '*' (at least on my system).