LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 02-26-2008, 05:00 AM   #1
sean_zhang
LQ Newbie
 
Registered: Aug 2007
Posts: 26

Rep: Reputation: 15
About the array variable


Code:
a=x
b=y
c=z
m=($a $b $c)
echo ${m[@]}
x y z
echo ${!m[@]}
0 1 2
This seems a little different w/ Bash reference manual.
http://www.gnu.org/software/bash/manual/

${!name[@]}
${!name[*]}
If name is an array variable, expands to the list of array indices (keys) assigned
in name. If name is not an array, expands to 0 if name is set and null otherwise.
When @ is used and the expansion appears within double quotes, each key
expands to a separate word.

And I found no matter what value is set to a, b, and c, the output of echo ${!m[@]} is always the same.
 
Old 02-26-2008, 05:30 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by sean_zhang View Post
And I found no matter what value is set to a, b, and c, the output of echo ${!m[@]} is always the same.
Right. Because bash assigns sequentially from zero.

Code:
:m=( x y z )

:echo ${m[@]}
x y z

:echo ${!m[@]}
0 1 2

:unset m[1] # destroy array member

:echo ${!m[@]}
0 2

:echo ${m[@]}
x z
The indices will always be 0-n, whatever their values, unless they don't exist.

Last edited by slakmagik; 02-26-2008 at 05:31 AM. Reason: left out an example
 
Old 02-26-2008, 09:57 PM   #3
sean_zhang
LQ Newbie
 
Registered: Aug 2007
Posts: 26

Original Poster
Rep: Reputation: 15
Thanks

Can you plz tell me what's meaning of "!" used in "echo ${!m[@]}".
It seems different w/ "!" in "echo ${!a@}", isn't it?
 
Old 02-27-2008, 01:01 AM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Right. The first is our familiar 'indexes of arrays' syntax. The second form is actually parameter expansion and has nothing to do with arrays, as such. It's just saying 'what variables are set that start with this string?'
Code:
:echo ${!H[@]}


:H=(a b c)

:echo ${!H[@]}
0 1 2

:echo ${!H@}
H HISTCMD HISTFILE HISTFILESIZE HISTIGNORE HISTSIZE HISTTIMEFORMAT HOME HOSTDISPLAY HOSTNAME HOSTTYPE HUSHLOGIN

:echo ${!HIST@}
HISTCMD HISTFILE HISTFILESIZE HISTIGNORE HISTSIZE HISTTIMEFORMAT
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash indirect reference to array variable(s) sixerjman Programming 6 10-25-2006 12:18 PM
Bash Local Variable Recursion With Array jshivers Programming 0 06-16-2006 05:31 PM
default array size for variable in gcc compiler mlaich Programming 13 01-04-2006 06:40 PM
Variable Array in C mojozoox Programming 6 12-16-2003 02:39 AM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 09:13 AM


All times are GMT -5. The time now is 12:38 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration