LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-21-2011, 11:03 AM   #1
flashback
LQ Newbie
 
Registered: Nov 2009
Location: Milan, Italy
Distribution: Fedora of course !
Posts: 11

Rep: Reputation: 0
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.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-21-2011, 12:02 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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 )
$ one=success
$ eval echo "\${${array[0]}}"
success
I do not think that you can avoid the 'eval' in this case.
 
1 members found this post helpful.
Old 10-21-2011, 12:11 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by crts View Post
not sure about what exactly you are trying to achieve but is this what you are trying to do?
He is trying to use the contents of a variable as an array name.
 
Old 10-21-2011, 12:26 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by MTK358 View Post
He is trying to use the contents of a variable as an array name.
In this case:
Code:
$ name=array
$ array=( one two )
$ eval echo \${$name[0]}
one
Or better:
Code:
$ eval echo \${${name}[0]}
The curly braces make it easier to see that 'name' is not an array but a normal variable.
Still, 'eval' cannot be avoided.
 
1 members found this post helpful.
Old 10-21-2011, 01:29 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
$ array=( one two )
$ echo "${!name[0]}"
one
Note however that using ${!name[@]} or ${!name[*]} will output a list of the all the existing index numbers of that array instead.

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
$ name=foo
$ array=( one two )
$ echo "${array[${!name}]}"
one
There are also now associative arrays, which are arrays that use character strings as indexes, rather than numbers. Most of the things that used to need indirect referencing can now be handled better with them.

Code:
$ declare -A array
$ array=( [one]=foo [two]=bar )

$ num=( one two )

$ echo "${array[${num[0]}]}"
foo
$ echo "${array[${num[1]}]}"
bar
More on indirect referencing here :
http://mywiki.wooledge.org/BashFAQ/006

Last edited by David the H.; 10-21-2011 at 02:27 PM. Reason: stated+some rewording
 
2 members found this post helpful.
Old 10-24-2011, 02:53 AM   #6
flashback
LQ Newbie
 
Registered: Nov 2009
Location: Milan, Italy
Distribution: Fedora of course !
Posts: 11

Original Poster
Rep: Reputation: 0
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]
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
arrays audreygay Linux - Newbie 3 05-26-2009 10:05 PM
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 01:39 PM
Arrays in C rubadub Programming 1 07-30-2007 02:26 PM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
help with arrays leroy27336 Programming 6 01-20-2004 06:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration