0

Code:

website="https://www.test.com/"
item_1="stuff"
item_2="more-stuff"

        while [1]
        do {
            for (i=1; $i<=3; i++)
              do {
                  echo $website$item_$i &
                  sleep 2
                 }
                 done
           }
           done

Output I'm getting:

https://www.test.com/1
https://www.test.com/2

Output I desire:

https://www.test.com/stuff
https://www.test.com/more-stuff

I am trying to increment the "item" variables by using $i in place of the number, so I may loop through them, but it is not giving me the desired results. Is there a way to make this possible?

Aaron
  • 3,135
  • 20
  • 51
  • 78
  • 1
    There is already a answer: https://unix.stackexchange.com/questions/41406/use-a-variable-reference-inside-another-variable – acrazing Aug 06 '17 at 03:01
  • @kcats would you be able to assist in helping me with what to put? I read it but Im not understanding what I should change. – Aaron Aug 06 '17 at 03:11

2 Answers2

3

Using item_x variables and inderect variable references:

#!/bin/bash

website="https://www.test.com/"
item_1="stuff"
item_2="more-stuff"

while true; do
    for (( i=1; i<=3; i++)); do
       item=item_$i
       echo $website${!item}
       sleep 2
    done
done

To create the indirect variable reference, first we have to create a variable that stores the name of the variable we want to indirectly reference, hence:

item=item_$i
# item stores the name of the variable we want  to 
# indirectly reference, be it item_1, item_2, etc.

Now that we have the name of the variable we want to reference in another variable, we use inderect reference to retrieve not the value of the variable item, but the value of the variable that is stored inside that variable, that is item_x:

${!item}

So var item stores the name of the variable we want to indirectly referenced by using the ${!var} notation.

It can be much simpler if you use an array instead:

#!/bin/bash

website=https://www.test.com/
items=( stuff more-stuff )

# You can refer to each item on the array like this:
echo ${items[0]}
echo ${items[1]}

while true; do
    for item in "${items[@]}"; do 
        echo "$website$item"
        sleep 2
    done
done

Also can try this other way:

#!/bin/bash

website="https://www.test.com/"
item[1]="stuff"
item[2]="more-stuff"

while true; do
   for (( i=1; i<=3; i++)); do
       echo $website${item[i]}
       sleep 2
   done
done
MauricioRobayo
  • 2,207
  • 23
  • 26
  • unfortunately the way i have it written in this example is a bit smaller than what i actually have, i need to have each "item" be its own variable. – Aaron Aug 06 '17 at 03:12
  • 1
    @Aaron notice that you can still refer to individual items in the array like so: `${items[0]}`, `${items[1]}`, etc. – MauricioRobayo Aug 06 '17 at 03:22
  • Indirect variable worked, can you explain how? its confusing me. – Aaron Aug 06 '17 at 04:57
  • 1
    In a function you can use the `declare -n` to create a *nameref* variable that would accomplish the same task. – David C. Rankin Aug 06 '17 at 05:05
  • 1
    @Aaron added an explanation about _indirect variable reference_ to the answer. English is not my first language so I apologize if it is not very clear or for any spelling or grammar mistake. – MauricioRobayo Aug 06 '17 at 14:31
0

You can use what we call indirect parameter expension ${!B} Two others threads are speaking about it.

How to get a variable value if variable name is stored as string?

Dynamic variable names in Bash

sheplu
  • 2,937
  • 3
  • 24
  • 21