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,059
  • 3
  • 29
  • 53
user267935
  • 365
  • 1
  • 4
  • 1
    Please Read [Security implications of forgetting to quote a variable in bash/POSIX shells](https://unix.stackexchange.com/q/171346/265604) –  Dec 29 '17 at 01:56
  • I don't think the down votes are all that appropriate, it is a well formatted and to the point question from a new user. – jesse_b Dec 29 '17 at 02:11

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,338
  • 16
  • 42
  • 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.