LinuxQuestions.org
Visit Jeremy's Blog.
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 04-17-2013, 07:09 AM   #1
sureshkumar.challa
LQ Newbie
 
Registered: Nov 2012
Location: Bangalore
Distribution: Ubuntu12.04
Posts: 6

Rep: Reputation: Disabled
Wink Array diclaration shell script


Hi Sir,

Please help me.

How to declare array in shell script?

i am using bash shell.

when i declared like this, It was showing error ---> "(" unexpected

Ex:-
#!/bin/bash
array_name=(value1 value2 value3)
echo {array_name[1]}

output:-
showiing error like ( Syntax error: "(" unexpected )
 
Old 04-17-2013, 07:15 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Although I do see a syntax error (missing $ sign), I'm not sure why you get this specific error.

It does seem to work on my side:
Code:
$ cat foo.sh
#!/bin/bash

array_name=(value1 value2 value3)
echo ${array_name[1]}

$ ./foo.sh
value2

$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
EDIT: Also works with bash version 3.1.17(1)

Last edited by druuna; 04-17-2013 at 07:21 AM.
 
Old 04-17-2013, 07:19 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Code:
#!/bin/bash
array_name=(value1 value2 value3)
echo ${array_name[1]}
works fine for me, and your original code doesn't give me an error either (both with bash 4.2.45). Have you copied and pasted your code into your post?
 
Old 04-17-2013, 07:34 AM   #4
nkiran
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Rep: Reputation: Disabled
You have missed "$"

Quote:
Originally Posted by sureshkumar.challa View Post
Hi Sir,

Please help me.

How to declare array in shell script?

i am using bash shell.

when i declared like this, It was showing error ---> "(" unexpected

Ex:-
#!/bin/bash
array_name=(value1 value2 value3)
echo {array_name[1]}

output:-
showiing error like ( Syntax error: "(" unexpected )
Your script works fine on my machine after a small correction. You have missed the $ before {array_name[1]}
#!/bin/bash
array_name=(value1 value2 value3)
echo ${array_name[1]}

Done!
I can't understand the relation between the error message and this correction. Can you copy paste your script here?
 
Old 04-17-2013, 08:56 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
You're probably not calling the script with a real bash. And I hope you're not actually trolling.
 
Old 04-17-2013, 09:27 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Once go through this article here.

You can declare arrary as:
Code:
arrary_name=(val1 val2 val3...)
Then use arrary as:
Code:
#!/bin/bash 
arrary_name=(val1 val2 val3...)
for val in "${arrary_name[@]}"
do
command1
command2
done

Last edited by shivaa; 04-17-2013 at 09:28 AM.
 
Old 04-18-2013, 03:59 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
My suspicion is that the OP is attempting to execute the script with sh directly, like this:

Code:
sh scriptname.sh
If so, then the #!/bin/bash shebang at the top of the script is being ignored and it's being parsed by /bin/sh instead. And if /bin/sh is symlinked to dash or a similar strictly posix shell, then arrays aren't supported.


If a script has the proper shebang, then just chmod it to be executable and launch it directly:

Code:
./scriptname.sh    #assuming pwd contains the script.
This way it will be interpreted by the shell specified in the shebang.


And please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
 
1 members found this post helpful.
  


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
Create Array in Shell Script dnoob Programming 13 10-20-2013 05:25 PM
array in shell script Abid Malik Programming 2 10-16-2010 02:49 PM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-26-2010 12:19 AM
Array shell script pooppp Linux - Networking 6 08-01-2008 09:37 AM
shell script array problem rche3252 Programming 1 10-09-2003 12:43 AM


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