I apologise for the pretty terrible title - and the poor quality post - but what I basically want to do is this:
for I in 1 2 3 4 echo $VAR$I # echo the contents of $VAR1, $VAR2, $VAR3, etc.
Obviously the above does not work - it will (I think) try and echo the variable called $VAR$I
Is this possible in Bash?
You can simply store the name of the variable in an indirection variable, not unlike a C pointer. Bash then has a syntax for reading the aliased variable: ${! name} expands to the value of the variable whose name is the value of the variable name . You can think of it as a two-stage expansion: ${!
No, you can't, and it makes no sense honestly to have a feature like that. Show activity on this post. No, and it doesn't make any sense. Variable (or more precisely, local) names are only important at compile-time, and they only have values at runtime.
Therefore, in your case, the variables will be passed from the main driver to the individual functions by reference. So, yes, you can have variables of the same name in different scopes.
Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name. Variable names may not be longer than 32 characters and are required to be shorter for some question types: multiselect, GPS location and some other question types.
Yes, but don't do that. Use an array instead.
If you still insist on doing it that way...
$ foo1=123 $ bar=foo1 $ echo "${!bar}" 123
for I in 1 2 3 4 5; do TMP="VAR$I" echo ${!TMP} done
I have a general rule that if I need indirect access to variables (ditto arrays), then it is time to convert the script from shell into Perl/Python/etc. Advanced coding in shell though possible quickly becomes a mess.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With