1

I'm trying to make a shell script but I'm stuck.

This doesn't work and I don't know why.

a=( "1" "2" )
b=( "3" "4" )
c=( "$a" "$b" )

for d in "${c[@]}"; do
    echo "${d[1]}"
done
5
  • In bash, array elements must be scalars, i.e., there's no multidimensional array in bash. Commented Dec 26, 2015 at 11:31
  • Do you know any way I can do something like this? Commented Dec 26, 2015 at 11:35
  • ksh93 has multidimensional arrays. Commented Dec 26, 2015 at 11:36
  • Thanks, I'll give it a look. Commented Dec 26, 2015 at 11:38
  • Have a look at this one: stackoverflow.com/questions/16487258/… and this one too: stackoverflow.com/questions/11233825/… Commented Dec 26, 2015 at 13:47

1 Answer 1

2

With bash:

a=( "1" "2" )
b=( "3" "4" )

# concatenate array a and b to new array c
c=( "${a[@]}" "${b[@]}" )

for d in "${c[@]}"; do 
  echo "$d"
done

Output:

1
2
3
4
Sign up to request clarification or add additional context in comments.

Comments

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.