0

I have this code:

for i in 'some     text' "some     other     text"
do
    echo $i
done

The output is:

some text
some other text

Why the spaces are not being printed?

Cyrus
  • 12,309

2 Answers2

0

Because it expands to

echo some text

which will result in the output you gave. That is why we should quote our variables in most cases. Try to replace echo $i with echo "$i" and see the difference.

Weijun Zhou
  • 3,368
  • I had thought that the command executed is still echo some(many spaces here)text but the spaces are trimmed when that command is executed. Did I miss something, or the concept that has been in my mind for long wrong? – Weijun Zhou Dec 29 '17 at 01:59
  • 1
    If this is the case, thank you very much for correcting my long-held belief. I will edit the answer. – Weijun Zhou Dec 29 '17 at 02:01
0

Quote your expansions echo "$i":

$ for i in 'some     text' "some     other     text"
do
    echo "$i"
done

Prints

some     text
some     other     text

Please read.