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