echo and double-space

echo and double-space

2021-01-24 1 By Denis Bisson

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:

See the difference between the two echo result

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!