Home > Back-end >  Want to assign split array value into another array using shell script
Want to assign split array value into another array using shell script

Time:12-15

I want to assign split array into another array and want to iterate nested array.

arg1="a,b,c,d"
arg2="1,2,3,4"
arg3="hi,hello,welcome"
arg4="hello,world"

IFS="," read -a val1 <<< ${arg1}
echo "val1 ${val1[@]}"
IFS="," read -a val2 <<< ${arg2}
echo "val2 ${val2[@]}"
IFS="," read -a val3 <<< ${arg3}
echo "val3 ${val3[@]}"
IFS="," read -a val4 <<< ${arg4}
echo "val4 ${val4[@]}"

operations[0]=${val1}
operations[1]=${val2}
operations[2]=${val3}
operations[3]=${val4}

echo "operations: ${operations}"

i have executed above code but how to assign nested array?

CodePudding user response:

You can try something like this:

a1=( a b c )
a2=( d e f )
a3=( g h j )

arr=(
    'a1[@]'
    'a2[@]'
    'a3[@]'
)

for i in "${arr[@]}"; { echo "${!i}"; }
a b c
d e f
g h j
  • Related