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 array.sh
array.sh: 2: array.sh: declare: not found
array.sh: 3: array.sh: myarray[0]=hey: not found
array.sh: 4: array.sh: myarray[1]=hello: not found
array.sh: 5: array.sh: myarray[2]=bye: not found
array.sh: 6: array.sh: Bad substitution

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:

bash array.sh

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


Be sure that your script is exectutable:

 chmod +x array.sh

And run it using the following command:


Your script is correct, but type:


instead of sh array.sh

The difference between ./ and sh is explained here.

  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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