Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I am trying to output the values of array element but I am getting weird output. Please have a look at it and help me solve it. Thank you.

declare -a myarray[$n]
for i in ${myarray[@]}
        echo $i


robin@robin-VirtualBox:~/lx$ sh 2: declare: not found 3: myarray[0]=hey: not found 4: myarray[1]=hello: not found 5: myarray[2]=bye: not found 6: Bad substitution
share|improve this question

2 Answers 2

up vote 1 down vote accepted

declare is a bash shell builtin, and is not defined in the sh shell.

So, you must to run your script using the following command:


Or add, the following shebang line at the start of your script:


Be sure that your script is exectutable:

 chmod +x

And run it using the following command:

share|improve this answer
Thank you for the explanation! –  Robin Apr 27 '14 at 0:12

Your script is correct, but type:


instead of sh

The difference between ./ and sh is explained here.

share|improve this answer
i'm getting permision denied. I even tried with sudo. –  Robin Apr 27 '14 at 0:08
Its ok, I had to make it executable. Thank you. –  Robin Apr 27 '14 at 0:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.