How can I split a string into array in shell script?
I tried with IFS='delimiter'
and it works with loops (for, while)
but I need an array from that string.
How can I make an array from a string?
Thanks!
str=a:b:c:d:e
set -f
IFS=:
ary=($str)
for key in "${!ary[@]}"; do echo "$key ${ary[$key]}"; done
outputs
0 a
1 b
2 c
3 d
4 e
Another (bash) technique:
str=a:b:c:d:e
IFS=: read -ra ary <<<"$str"
This limits the change to the IFS variable only for the duration of the read command.