I am using Bash 5.0.17 on Ubuntu 20.04
When I run the following commands:
IFS=":"; for i in "1:2:3"; do echo $i; done
# output is: 1 2 3
IFS=":"; for i in "1:2:3"; do echo "$i"; done
output is: 1:2:3
IFS=":"; for i in "1:2:3"; do printf "%s\n" $i; done
output is:
1
2
3
IFS=":"; for i in "1:2:3"; do printf "%s\n" "$i"; done
output is: 1:2:3
This is confusing for me.
- Why doesn't
echo
print each token in a separate line? - Why does
printf
works as expected when$i
is not quoted? - Why both
echo
andprintf
fail when$i
is quoted?
I appreciate your help
IFS=":"
cause the string "1:2:3" to split to three tokens so the for loop iterates over it 3 times? that's the key question – Amazigh_05 Dec 19 '21 at 20:49