0

I'm working in a GitLab runner environment and CICD Variables with two familiar strings in my bash script and I would change one of these strings in the middle to build a final string. For example:

$CICD_MY_INTERNAL_STRING
$CICD_MY_EXTERNAL_STRING

So now these strings are in my function "workerapp()" and I would make this string dynamic.

function workerapp() {
echo -e $CICD_MY_$1_STRING
}

Now I would call the function like this

workerapp INTERNAL
workerapp EXTERNAL

to get these results

"$CICD_MY_INTERNAL_STRING" and "$CICD_MY_EXTERNAL_STRING" to work with it in another functions / calls.

Currently I got only these results "$CICD_MY_EXTERNAL" ... without the rest of my strings.

Patrick
  • 1
  • 1

1 Answers1

1

You could use the ${!varname} construct.

workerapp() {
    local varname="CICD_MY_$1_STRING"
    echo -e "${!varname}"
}

workerapp INTERNAL
workerapp EXTERNAL
karolba
  • 956
  • 10
  • 19
  • Thanks for your help. My solution is: ```workerapp() { local varname="CICD_MY_$1_STRING" echo -e "${!varname}" } workerapp INTERNAL workerapp EXTERNAL ``` – Patrick Jun 10 '22 at 12:06
  • @Patrick If you think I've helped you, you can accept my answer. If you want to, click on the check mark beside the answer to toggle it from greyed out to filled in. – karolba Jun 13 '22 at 17:32