0

I'm a bit new to Bash programming, but I was curious during my learning so I wanted to ask what is the logic (or reason) behind as to why:

#/bin/sh
ITERS="1 2 3 4"
for i in $ITERS;
do
    echo hi
done

echos hi 4 times, while

#/bin/sh
for i in "1 2 3 4";
do
    echo hi
done

Only echos it once? And how can I achieve the above behaviour without declaring a variable?

q.Then
  • 2,743
  • 1
  • 21
  • 31
  • Are you looking for, simply, `for i in 1 2 3 4`? – tripleee Apr 13 '16 at 06:27
  • yes, however, why can't the string also work? – q.Then Apr 13 '16 at 06:29
  • Because then you are looping over the single, quoted string, which is one value. – tripleee Apr 13 '16 at 06:30
  • Incidentally, your question is tagged `bash`, but your examples indicate `/bin/sh`. They are different shells, with different features; even when `/bin/sh` is a symlink to Bash, it disables (some) Bash-only features. – tripleee Apr 13 '16 at 06:31
  • @tripleee is right, look at the [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) – sobolevn Apr 13 '16 at 06:36
  • It doesn't actually matter here, though; Bash is perfectly backward-compatible, and there are no Bash features in this script. – tripleee Apr 13 '16 at 06:37
  • I'm tempted to mark this question as a duplicate. If the linked question answers your concerns, I'll just go ahead and do that. If not, please [edit] your question to clarify what sort of background you require. Quoting is a sticky topic but writing another tutorial seems excessive, as there are tons of them already. – tripleee Apr 13 '16 at 06:41

0 Answers0