Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading