LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 06: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, 06:15 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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 06:21 AM.
 
Old 04-17-2013, 06:19 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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, 06: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, 07: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, 08: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 08:28 AM.
 
Old 04-18-2013, 02:59 PM   #7
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
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



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 04:25 PM
array in shell script Abid Malik Programming 2 10-16-2010 01:49 PM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
Array shell script pooppp Linux - Networking 6 08-01-2008 08:37 AM
shell script array problem rche3252 Programming 1 10-08-2003 11:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:28 AM.

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