echo and double-space
Today I learned something regarding what is output when using echo
command with a string values with a double-space in the same.
I came across a situation today where the echo
command was not outputting the result that was expected and it was regarding variables with a string value with two spaces in a row.
Let’s suppose we have a bash script with this line:
echo $sOutputLine
If the value of $sOutputLine is a string with a double-space in it, the actual output of echoing out will be a result where the double-space will be simplified to one space!!!
If we want to have the string value given back as it was, we need to quote it like this:
echo "$sOutputLine"
Here is a screen capture showing it:
This is an easy scenario to see the difference. Today it happened to me I did not quote the variable since in fact I was not aware of that and it was a bit more complicated because it was integrated in something else and I did not know the problem was coming from that echo
:
I guess if I learn one thing a day, I’ll finally maybe one day be able to write a working script the first time!
Thanks for sharing!
I think it already happened to me once long time ago.
I did not realize it was related to that and I ended up doing things differently.
But I think that was problem.
It’s funny to see an explanation like five years later! 🙂