Why does the following...
c=0; for i in $'1\n2\n3\n4'; do echo iteration $c :$i:; c=$[c+1]; done
print out...
iteration 0 :1 2 3 4:
and not
iteration 0 :1:
iteration 1 :2:
iteration 2 :3:
iteration 3 :4:
From what I understand, the $'STRING' syntax should allow me to specify a string with escape characters. Shouldn't "\n" be interpreted as newline so that the for loop echos four times, once for each line? Instead, it seems as if the newline is interpreted as a space character.
I took unwind's suggestion and tried setting $IFS. The results were same.
IFS=$'\n'; c=0; for i in $'1\n2\n3\n4'; do echo iteration $c :$i:; c=$[c+1]; done; unset IFS;
iteration 0 :1 2 3 4:
William Purssel says in a comment that this did not work because IFS was being set to newline... but following did not work.
IFS=' '; c=0; for i in '1 2 3 4'; do echo iteration $c :$i:; c=$[c+1]; done; unset IFS;
iteration 0 :1 2 3 4:
Using IFS=' ' on newline-separated string resulted in even more mess...
IFS=' '; c=0; for i in $'1\n2\n3\n4'; do echo iteration $c :$i:; c=$[c+1]; done; unset IFS;
iteration 0 :1
2
3
4:
setting IFS to '\n' rather than $'\n' had the same effect as IFS=' ' ...
IFS='\n'; c=0; for i in $'1\n2\n3\n4'; do echo iteration $c :$i:; c=$[c+1]; done; unset IFS;
iteration 0 :1
2
3
4:
There's only one iteration, but the newline is visible in the echo for some reason.
What did work is first storing the string in a variable then looping over the contents of the variable (without having to set IFS):
c=0; v=$'1\n2\n3\n4'; for i in $v; do echo iteration $c :$i:; c=$[c+1]; done
iteration 0 :1:
iteration 1 :2:
iteration 2 :3:
iteration 3 :4:
Which still does not explain why there is this problem.
Is there a pattern here? Is this the expected behavior of IFS as defined in unwind's link?
unwind's link states... "The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting."
I guess that explains why string literals don't get split for for-loop iteration no matter what escape characters are used. Only when the literal is assigned to a variable then that variable is expanded to be split for the for-loop does it work. I guess also with command substitution.
Examples:
Result of command substitution is split
c=0; for i in `echo $'1\n2\n3\n4'`; do echo iteration $c :$i:; c=$[c+1]; done
iteration 0 :1:
iteration 1 :2:
iteration 2 :3:
iteration 3 :4:
Portion of the string that was expanded is split, rest is not.
c=0; v=$'1 \n\t2\t3 4'; for i in $v$'\n5\n6'; do echo iteration $c :$i:; c=$[c+1]; done
iteration 0 :1:
iteration 1 :2:
iteration 2 :3:
iteration 3 :4 5 6:
When expansion happen in double quotes, no splitting occurs.
c=0; v=$'1\n2\n3 4'; for i in "$v"; do echo iteration $c :$i:; c=$[c+1]; done
iteration 0 :1 2 3 4:
Any sequence of SPACE, TAB, NEWLINE is used as delimiter for splitting.
c=0; v=$'1 2\t3 \t\n4'; for i in $v; do echo iteration $c :$i:; c=$[c+1]; done
iteration 0 :1:
iteration 1 :2:
iteration 2 :3:
iteration 3 :4:
I will accept unwind's answer as his link yields the answer to my question.
No clue as to why behavior of echo within for-loop changes with value of IFS.
EDIT: extended to clarify.