0

How to pass the value from variable inside the array list.

The below code works, it assigns the repo_libs elements to repo_list. Is there a way to read the value from variable to form the array name inside the array list.

 if [[ "$service" == "all" || "$service" == "libs" ]] ; then
    # below working        
    repo_list=("${repo_libs[@]}")
        echo "repo_list : ${repo_list[*]}"
 fi

Looking for statements like below

repo_list=(${repo_`echo $service`[@]})
repo_list=("${repo_$service[@]}")

seeing the error like below.

./build.sh: line 168: ${repo_`echo $service`[@]}: bad substitution
./build.sh: line 173: ${repo_$service[@]}: bad substitution

[EDIT] Here is the code using if .. else as an alternative approach that I am using.

 if [ "$service" = "all" ] ; then
        repo_list=("${repo_all[@]}")
 elif [ "$service" = "libs" ] ; then
        repo_list=("${repo_libs[@]}")
 elif [ "$service" = "portlets" ] ; then
        repo_list=("${repo_portlets[@]}")
 else
        repo_list=("$service")
 fi
intechops6
  • 1,007
  • 4
  • 22
  • 43
  • @that other guy - could you say how this is duplicate of https://stackoverflow.com/questions/4582137/bash-indirect-array-addressing This is not about how to assign one array to another. This is about variable expansion inside array list. – intechops6 Jun 06 '19 at 21:03
  • You have a variable `service="libs"` and want to use that to access an array named `repo_$service` (so `repo_libs`), right? That's exactly the same as the duplicate. – that other guy Jun 06 '19 at 21:12
  • you are right, but, I believe the problem statement is not same with both the questions and answers in another question is not addressing the question here. maybe someone else can say whether it is duplicate or not. – intechops6 Jun 06 '19 at 21:26
  • Use associative arrays. And see [BashFAQ/006](http://mywiki.wooledge.org/BashFAQ/006). – Dennis Williamson Jun 06 '19 at 21:27
  • I am having multiple arrays and all the arrays have similar values that have to be assigned to another array based on the input. – intechops6 Jun 06 '19 at 22:32

0 Answers0