LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-15-2006, 01:21 PM   #1
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Rep: Reputation: 45
BASH - Array Variables


I am learning about array variables. So according to my book, I do this:

Code:
name=(matt mark luke john xena)
is equivalent to:
Code:
name[0]=matt
name[1]=mark
name[2]=luke
name[3]=john
name[4]=xena
However, when I then do:
echo $name[2], I should get name[2]=luke
Instead, I get matt[2]. When I try echo $name[4], I get matt[4]

What's going on??
 
Old 12-15-2006, 01:22 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 64
Try separating each element with a comma (,).
Also, I'm pretty sure you need to declare $name as an array (I'm not sure how to do this).

Last edited by indienick; 12-15-2006 at 01:26 PM.
 
Old 12-15-2006, 01:33 PM   #3
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
adding the comma did not work. It then came out to
echo $name[1] shows name[1]=matt,

I think I am doing something wrong, because even if I set the array manually, it still doesn't work.
name[0]=matt
name[1]=mark
name[2]=luke
name[3]=john
name[4]=xena

Then I do echo $name[1], I get name[1]=matt. This is confusing me. Maybe I should skip this chapter??
 
Old 12-15-2006, 03:24 PM   #4
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 64
Hmm....

When I started messing around with BASH scripting, I learned from here.
It never failed me.
 
Old 12-15-2006, 04:28 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 706

Rep: Reputation: 68
Hi.
Code:
       Any  element  of  an  array may be referenced using ${name[subscript]}.
       The braces are required to avoid conflicts with pathname expansion.

 -- man bash
Best wishes ... cheers, makyo
 
Old 12-15-2006, 05:49 PM   #6
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by makyo
Hi.
Code:
       Any  element  of  an  array may be referenced using ${name[subscript]}.
       The braces are required to avoid conflicts with pathname expansion.

 -- man bash
Best wishes ... cheers, makyo
PERFECT! Adding the braces made it work. THANK YOU!!!! I just realized that my book used an example with the brace, but I overlooked it.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help With Variables In Bash jamie_h Programming 3 11-01-2006 08:18 AM
Array Help in BASH! ?*%$ johnnybhoy67 Linux - Software 2 02-22-2006 10:39 AM
bash variables pfaendtner Linux - Newbie 4 11-23-2004 01:00 PM
Variables in bash tcaptain Programming 1 03-03-2003 02:07 PM
Bash variables pk21 Programming 2 01-09-2003 03:31 PM


All times are GMT -5. The time now is 02:59 PM.

Main Menu
 
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
Open Source Consulting | Domain Registration