Nested environment variables

I have some files uploaded in the generic file store and in my workflow I have a file download step. Now depending on a variable called “society” I need to replace part of the environment variable name to download. Basically, I need to do something like:

$BITRISEIO_firebase_info_${society}_URL

Which would give me something like

$BITRISEIO_firebase_info_AUS_URL

Which then expands to a URL to download a file.

As far as I can tell, it’s not possible to have a variable expand inside of another variable. I tried a few different ways and my file download step always errored.

Any tips/advice would be appreciated.

1 Like

Hello there! well this is not really possible, but you could just make two separate env vars for the two urls.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

Actually it can work with Bash indirect ref:

#!/usr/bin/env bash
set -ex

export society=AUS
export BITRISEIO_firebase_info_AUS_URL='https://some.aus.url'

full_env_var_key=BITRISEIO_firebase_info_${society}_URL

echo "key: $full_env_var_key"
echo "value: ${!full_env_var_key}"

value_as_a_normal_variable="${!full_env_var_key}"
echo "value_as_a_normal_variable: ${value_as_a_normal_variable}"

(based on https://stackoverflow.com/questions/16553089/dynamic-variable-names-in-bash ).

If you then want to use the value in another step then you can expose the result to a new env var, e.g. CURRENT_FIREBASE_INFO_URL which then can be used in step inputs directly:

#!/usr/bin/env bash
set -ex

export society=AUS
export BITRISEIO_firebase_info_AUS_URL='https://some.aus.url'

full_env_var_key=BITRISEIO_firebase_info_${society}_URL

echo "key: $full_env_var_key"
echo "value: ${!full_env_var_key}"

value_as_a_normal_variable="${!full_env_var_key}"
echo "value_as_a_normal_variable: ${value_as_a_normal_variable}"

bitrise envman add --key CURRENT_FIREBASE_INFO_URL --value "$value_as_a_normal_variable"

If don’t need actual final variable but only its value you can use more explicit approach like this:
printenv BITRISEIO_firebase_info_${society}_URL
Note there is no leading $.
Usage with envman:
envman add --key FOO --value $(printenv BITRISEIO_firebase_info_${society}_URL)