How to evaluate a variable inside a variable bash scripting?

buzzinolops

I have a bunch of arrays like this:

array1=("A" "B")
array2=("C" "D")
array3=("E" "F" "G")

And I want to loop over the arrays, and the elements in each. Here is how I'm trying to accomplish this:

for i in `seq 1 2`
do 
    for elm in ${array${i}[@]}
    do
        echo "the element in array$i is $elm" 
    done
done

But, this gives me:

./new_test.sh: line 6: ${array${i}[@]}: bad substitution

I sort of know that what I'm doing is wrong, because I don't want the first $ to evaluate the ${i} inside of it.

How do I prevent this?

Jahid

This should work:

var=array$i[@]
for elm in ${!var}
...

Example:

#!/bin/bash
array1=("A" "B")
array2=("C" "D")
array3=("E" "F" "G")

for i in `seq 1 2`
do 
    var=array$i[@]
    for elm in "${!var}"
    do
        echo "the element in array$i is $elm" 
    done
done

Output:

the element in array1 is A
the element in array1 is B
the element in array2 is C
the element in array2 is D


Indirect expansion (from bash manual):

If the first character of parameter is an exclamation point (!), it introduces a level of variable indirection. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

BASH scripting, proper quoting of variable inside sed?

From Dev

How to include and or evaluate a variable in bash?

From Dev

Bash scripting: if variable equals to string

From Dev

Bash scripting: variable is not incrementing correctly

From Dev

How to store the array length in a variable in bash scripting language

From Dev

How to store the array length in a variable in bash scripting language

From Dev

jQuery how to evaluate variable

From Dev

U-Boot: How to evaluate one environment variable inside another

From Dev

How can I evaluate a variable from an AngularJS controller inside an html?

From Dev

U-Boot: How to evaluate one environment variable inside another

From Dev

How can I evaluate a variable from an AngularJS controller inside an html?

From Dev

BASH variable and awk inside variable

From Dev

BASH variable and awk inside variable

From Dev

How to compare variable dynamically inside awk in bash?

From Dev

In Bash, how to set stdout to a variable inside tee

From Dev

How redirect stderr to variable inside if condition? Bash

From Dev

How to declare a variable inside bash -c

From Dev

Bash scripting grep variable from multilined json?

From Dev

Variable comparison inside if in Bash

From Dev

Bash variable inside awk

From Dev

PhantomJS; If I set a variable inside page.evaluate(), how can I access the value of that variable outside of page.evaluate()

From Dev

In a bash script, how to use a variable inside a command and assign to a new variable?

From Dev

How to write if inside another if in bash scripting language

From Dev

Variable not expanding inside another variable bash

From Dev

Variable not expanding inside another variable bash

From Dev

shell scripting: how to cut line in variable

From Dev

shell scripting: how to cut line in variable

From Dev

bash: variable expansion inside var=$' '

From Dev

Incrementing a variable inside a Bash loop

Related Related

  1. 1

    BASH scripting, proper quoting of variable inside sed?

  2. 2

    How to include and or evaluate a variable in bash?

  3. 3

    Bash scripting: if variable equals to string

  4. 4

    Bash scripting: variable is not incrementing correctly

  5. 5

    How to store the array length in a variable in bash scripting language

  6. 6

    How to store the array length in a variable in bash scripting language

  7. 7

    jQuery how to evaluate variable

  8. 8

    U-Boot: How to evaluate one environment variable inside another

  9. 9

    How can I evaluate a variable from an AngularJS controller inside an html?

  10. 10

    U-Boot: How to evaluate one environment variable inside another

  11. 11

    How can I evaluate a variable from an AngularJS controller inside an html?

  12. 12

    BASH variable and awk inside variable

  13. 13

    BASH variable and awk inside variable

  14. 14

    How to compare variable dynamically inside awk in bash?

  15. 15

    In Bash, how to set stdout to a variable inside tee

  16. 16

    How redirect stderr to variable inside if condition? Bash

  17. 17

    How to declare a variable inside bash -c

  18. 18

    Bash scripting grep variable from multilined json?

  19. 19

    Variable comparison inside if in Bash

  20. 20

    Bash variable inside awk

  21. 21

    PhantomJS; If I set a variable inside page.evaluate(), how can I access the value of that variable outside of page.evaluate()

  22. 22

    In a bash script, how to use a variable inside a command and assign to a new variable?

  23. 23

    How to write if inside another if in bash scripting language

  24. 24

    Variable not expanding inside another variable bash

  25. 25

    Variable not expanding inside another variable bash

  26. 26

    shell scripting: how to cut line in variable

  27. 27

    shell scripting: how to cut line in variable

  28. 28

    bash: variable expansion inside var=$' '

  29. 29

    Incrementing a variable inside a Bash loop

HotTag

Archive