0

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?

3
  • bash does not support nested arrays. Commented Dec 15, 2022 at 7:02
  • any other way to store and iterate these objects Commented Dec 15, 2022 at 7:08
  • Arrays are not present at all in the POSIX sh standard. Why is this question tagged sh and not bash? Commented Dec 15, 2022 at 18:53

1 Answer 1

1

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

Slicing also possible and spaces preserved:

a1=( a 'b 1' c )
a2=( d 'e 2' f )
a3=( g 'h 3' j )

$ for i in "${arr[@]}"; { echo "${!i:0:1}"; }
a
d
g

$ for i in "${arr[@]}"; { echo "${!i:1:1}"; }
b 1
e 2
h 3

$ for i in "${arr[@]}"; { echo "${!i:2:1}"; }
c
f
j
Sign up to request clarification or add additional context in comments.

1 Comment

thanks this way only doing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.