About arrays
Hi all, i have my first question here in LQ, and it's related to arrays and bash.
Here we go: is it possible to use instead of the plain following: echo ${test[0]} to read the first element of test, that is obviously a previously declared array, something like: echo ${$myvar[0]} where myvar is a variable containing the name of an array ? Should i instead play with eval and string concatenation ? Thanks for any answer, Roberto. |
Welcome to LQ,
not sure about what exactly you are trying to achieve but is this what you are trying to do? Code:
$ array=( one two ) |
Quote:
|
Quote:
Code:
$ name=array Code:
$ eval echo \${${name}[0]} Still, 'eval' cannot be avoided. |
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.
Your question really has little to do with arrays specifically. What you're asking for is called indirect referencing. eval is one way to do it, but it's certainly not recommended usually. eval has security issues. In a nutshell, you should never use it on any string where you don't have complete control over the contents, and can't determine what the actual eval'd command would look like and do. http://mywiki.wooledge.org/BashFAQ/048 Depending on what your final purpose is, there are a few other, probably better, options. To start with, bash has a dedicated indirect reference expression "${!variable}". To re-use the above example: Code:
$ name=array Edit: D'oh! I just realized this doesn't quite work as expected. ${!name[0]} evaluates to "one", because it's the same as ${!name}. But ${!name[1]} does not evaluate to anything, as it's trying to reference a non-existent array entry. In short, it will work when expanding simple variables, but can't be used for dynamic array names. You could however indirectly reference the index number instead, although now we're getting really nested! Code:
$ foo=0 Code:
$ declare -A array http://mywiki.wooledge.org/BashFAQ/006 |
Thanks
Thanks to everybody for you answers, i have now all the hints i need to proceed.
MTK358 was right, in fact i was trying to use the contents of a variable as an array name. Thanks again to crts and David. Roberto. p.s. next time i'll use [code] :) |
All times are GMT -5. The time now is 06:52 PM. |