4

I have been searching everywhere for an answer to this question. I have an array in my shell script, but when I run it, I get this error: "(" unexpected

What am I doing wrong here:

array=( 1 2 3 4 5 )

I am using Ubuntu 11.10

4 Answers 4

10

You are running your script with /bin/sh, not /bin/bash. There are no arrays in sh.

choroba@cyan ~$ /bin/sh
$ a=( 1 2 3 )
/bin/sh: Syntax error: "(" unexpected
Sign up to request clarification or add additional context in comments.

Comments

0

are you using bash?

$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.
$ array=( 1 2 3 4 5 )
$ echo ${array[1]}
2
$ 

4 Comments

Yes, this works fine. However, the script isn't running for some reason.
Weird... When I copy paste entire script into terminal, it works. Why doesn't work as an external file?
@user1068559 you can try running the script with sh --verbose -x script.sh and see where it actually breaks tldp.org/LDP/abs/html/arrays.html might help you as well
Hmm... not revealing much unfortunately. I know the script works, so it must be some kind of quirk with something else.
0

Also sometime it's because of extra space:

array = (1 2 3 4)

is not right. It should be:

array=(1 2 3 4)

Comments

0

Try specifying the word bash when running the script as follows:

$ bash script.sh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.