0

I'm developing a program and I need to create a function that for each "VARIANT$X" that exist, execute the "make_anykernel" function, I'm trying to do it with a variable named VARIANT$X, "X" starts with the value = 1 and each cycle X = X + 1 to check if the next VARIANT has a value and then execute the function again, the problem is that the variable "VV" isn't taking the value of VARIANT$X, is taking literally VARIANT1, VARIANT2, VARIANT3... Here's my code

X=1
bool=true
while [ "$bool" = true ]; do
  export VARIANT=VARIANT$X
  export DEFCONFIG=DEFECONFIG$X
  X=$((X+1))
  VARIANT$X=VV
  if [ "$VV" = "" ]; then
    bool=false
  else
    make_anykernel
  fi
done

Sorry if this is a stupid question or problem, any help will be very grateful, thanks.

2 Answers2

0

You can use variable indirection:

#!/usr/bin/env bash

variant1=foo
variant2=bar

i=1
while var=variant$((i++)); [[ ${!var} ]]; do
    echo "The value of $var: ${!var}"
done
PesaThe
  • 7,259
  • 1
  • 19
  • 43
0

I think ultimately you are looking for ${!var}:

$ a1="this is a1"; n=1; b=a$n; echo ${!b}
this is a1

As an aside, the [ "$bool" = true ] is redundant. You can more concisely use "$bool" directly as in:

while "$bool"; do

since true and false are commands that return success and failure.

Brian
  • 92
  • 1
  • 5