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
echoprint each token in a separate line? - Why does
printfworks as expected when$iis not quoted? - Why both
echoandprintffail when$iis 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