0

I would like to concatenate unlimited numbers of arrays using shortest lines possible, so for this I did the code below:

#!/bin/bash
declare -a list1=("element1")
declare -a list2=("element2")
declare -a list3=("element3")
declare -a list4=("element4")
declare -a list
for i in {1..4}
do
   list=( ${list[@]} ${list$i[@]} )
done
echo ${list[*]}

But the code above is not working because $i is not seen as variable and the error is: ${list$i[@]} bad substitution

4
  • using shortest lines then why do you use so many spaces? to concatenate unlimited numbers you want to output them or store them in an array? Please be specific. Commented Jan 18, 2022 at 9:23
  • Unlimited? Do you really want to create an unlimited number of variables? Commented Jan 18, 2022 at 9:27
  • do not care of spaces, I used for better view. I want to have all elements in one array first, surely I will use elements later but how could I use a variable as number in array name for this concatenation? Commented Jan 18, 2022 at 9:27
  • just saying unlimited number of arrays, this is limited to 10 at first but later I want to add mores so they could be 100 Commented Jan 18, 2022 at 9:28

2 Answers 2

1

You can use variable indirection:

for i in {1..4} ; do
    ref="list$i[@]"
    list+=("${!ref}")
done
echo "${list[@]}"
Sign up to request clarification or add additional context in comments.

3 Comments

You need double-quotes around ${!ref} -- without them, it'll be subject to the usual word-splitting and wildcard-expansion messiness.
not working, I have no error but when printing using echo I have just first element1
@antonio1: Weird, it works for me with your example.
0

The following code outputs all 4 lists concatenated together.

eval echo \${list{1..4}[*]}

This code runs filename expansion over the result of list elements (* is replaced by filenames). Consider sacrificing 4 characters and doing \"\${list{1..4}[*]}\".

Note that eval is evil https://mywiki.wooledge.org/BashFAQ/048 and such code is confusing. I wouldn't write such code in a real script - I would definitely use a loop. Use shellcheck to check your scripts.

2 Comments

I'm sorry but something is not good, finally I get all elements in one array but they don't have double quotas anymore and when I try to use the final array with a for loop to get elements one by one it is seen all elements as one. This is not good.
The code I presented does not put elements in an array. It outputs them. As I understood, the goal was to output them in shortest lines possible. finally I get all elements in one array then you are not using the code I presented in this answer (?). Do you actually need for it be shortest as possible? See the other answer - it properly handles quoting. Check your scripts with shellcheck

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.