First, a simple situation, echo "$@"
directly in a for loop:
#!/bin/bash
set -- "First one" "second" "third:one" "" "Fifth: :one"
IFS=:
# IFS=":", using "$@"
c=0
for i in "$@"
do echo "$((c+=1)): [$i]"
done
Output, as expected:
1: [First one]
2: [second]
3: [third:one]
4: []
5: [Fifth: :one]
But when I assign "$@"
to a variable var
and then echo $var
in a for loop, things become complicated:
#!/bin/bash
set -- "First one" "second" "third:one" "" "Fifth: :one"
IFS=:
# IFS=":", using $var (var="$@")
var="$@"
c=0
for i in $var
do echo "$((c+=1)): [$i]"
done
It Print:
1: [First one second third]
2: [one Fifth]
3: [ ]
4: [one]
Why output changed? Can anybody tell me what happened under the hood when I add var="$@"
and then echo $var
in a for loop?