Sample Header Ad - 728x90

Synthetic Variables in Bash (was: How to address a variable ceated with indirection)

1 vote
4 answers
116 views
Sorry, that was all too confusing, (my bad) Let me try to explain again, but with a more simple example:
#define a new var and export it to the "env":
export VAR_ONE=thisIsVarOne
# check if it was created:
env | grep VAR_ONE      #this displays: "VAR_ONE=thisIsVarOne"
# use it in a simple echo command:
echo VAR_ONE=$VAR_ONE  #this displays: "VAR_ONE=thisIsVarOne"

# create a new var with the name that contains the value of VAR_ONE ("newvarthisIsVarOne") in its name:
export newvar${VAR_ONE}="somePrefix${VAR_ONE}"  
# verify if it has been created:
env | grep newvar  # this displays: "newvarthisIsVarOne=somePrefixthisIsVarOne"
### Now my question: How do I use that new, syntetic, variable? Please note that: env | grep newvar shows that there has been created a variable with the name newvar${VAR_ONE} But I can't **address** that new variable, e.g. in an echo command. I tried: echo ${newvar${VAR_ONE}} But that gave me bash: ${newvar${VAR_ONE}}: bad substitution ### Constraints: 1. The value of VAR_ONE can change, so I can't use that to address the newly created variable 2. I can't declare a new variable with a static name. For usage, further in the script, I need the keyword VAR_ONE as part of the name of the newly created variable. The name of the variable needs to be something like: newvar${VAR_ONE} Thank you in advance ChriV
Asked by Chris V. (21 rep)
Nov 17, 2023, 06:11 PM
Last activity: Nov 18, 2023, 05:26 AM