I have an array my_array
in Bash that contains string elements formatted as such
my_array[0] = "username_1:id_1:name_1"
my_array[1] = "username_2:id_2:name_2"
my_array[2] = "username_3:id_3:name_3"
I was curious if there is a convenient way to iterate through this array splitting it on the ":" character and storing the values into three separate arrays. The pseudo-code might look something like this
index=0
for i in "${my_array}"
do
usernames[$index], ids[$index], names[$index]= $(echo $i | tr ":" "\n")
(( index = index + 1 ))
done
This question (How do I split a string on a delimiter in Bash?) is very similar to what I am trying to accomplish, however I am trying to store the values into three different arrays which is my main obstacle. Thanks in advance for any help as I am extremely new to bash programming.
here is what I have attempted:
index=0
for i in "${arrayAdmin[@]}"
do
credentials=$(echo $i | tr ":" "\n" )
accounts[$index]=${credentials[0]}
groups[$index]=${credentials[1]}
names[$index]=${credentials[2]}
(( index = $index + 1))
echo $index
done