I am learning about the for loop in bash
, I have found examples such as the following online:
for i in 1 2 3 4 5
do
echo $i
done
I replaced the 1 2 3 4 5
with many "things" (numbers, strings, variables, etc.):
myVar="!!"
myVar2="Bye"
for i in 3 15 1 32 6 "Hello World $myVar" 'Hello World in single quotes' Hi $myVar2 $(ls)
do
echo $i
done
When I run the script for the above code, it worked as expected. But is it invalid in some way to mix all these "things" together?
$(ls)
rather than just using*
.for i in $(ls)
is an unfortunately common error. – cas Dec 25 '17 at 10:36$myVar2
contained the string"Hello World"
, and I used"$myVar2"
(with double quotes) in the loop, then the"Hello World"
string will be treated as a single argument and"Hello World"
will be printed on a single line, but if I used$myVar2
(without double quotes) in the loop, then"Hello"
will be treated as an argument and printed on its own line, and"World"
will be treated as an argument and printed on its own line? – user267365 Dec 25 '17 at 17:28