1
#!/bin/bash
ARRAYNAME=( 'time1' 'life' 'time2' )
echo ${ARRAYNAME[1]}

In the above code when i run my script as

$ sh ex1.sh

it gives an error message:

ex1.sh: 2: Syntax error: "(" unexpected

Why is this?

1
  • 1
    Run bash ex1.sh when you want to use bash features (not found in the POSIX shell). Commented Nov 7, 2011 at 5:18

2 Answers 2

5

sh on your system is not bash.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly. The #!/bin/bash says to run the script using /bin/bash, but by running it with sh ex1.sh, you ignore that and run it using sh. Just run it as ./ex1.sh. (And the .sh suffix isn't really needed.)
0

Your "shebang" lines uses bash shell ("/bin/bash") but you're probably invoking another shell ("sh") invoking another shell to execute your script. Try this :

$ chmod 700 ex1.sh

This will make your script executable. Then run it :

$ ./ex1.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.