# Displaying Array Elements in Shell Scripts

**URL:** <https://community.unix.com/t/displaying-array-elements-in-shell-scripts/212684>\
**Category:** Shell Programming and Scripting\
**Created:** [September 16, 2008, 7:31am UTC](https://community.unix.com/t/displaying-array-elements-in-shell-scripts/212684 "2008-09-16T07:31:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ananddr](https://community.unix.com/letter_avatar/ananddr/32/6_449c533f586939b6e5f246632a46421f.png) [@ananddr](https://community.unix.com/u/ananddr)\
**Post date:** [September 16, 2008, 7:31am UTC](https://community.unix.com/t/displaying-array-elements-in-shell-scripts/212684/1 "2008-09-16T07:31:22Z")

</div>

Hi All,

I am using the following piece of script to print all the array elements in a script by name compare.sh:

31 len=${#array  
[\*]};  
32 j=0;  
33 #echo "The length of the array is : $len"  
34 while [$j -lt $len]; do  
35 temp=${array[$j]}  
36 echo "$temp"  
37 let $j++  
38 done

But I am getting the following error.

ksh: ./compare.sh[38]: [0: not found

I couldn't understand what is happening...

Can anyone help me on this.....

Thanks in advance

Anand.

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/6_449c533f586939b6e5f246632a46421f.png) [@era](https://community.unix.com/u/era)\
**Post date:** [September 16, 2008, 7:41am UTC](https://community.unix.com/t/displaying-array-elements-in-shell-scripts/212684/2 "2008-09-16T07:41:46Z")

</div>

You need a space after the [ in the while -- [ is the name of a command (!) and needs to be written with a space after it, just like any other command.

---

<div class="post-metadata">

**Author:** ![ananddr](https://community.unix.com/letter_avatar/ananddr/32/6_449c533f586939b6e5f246632a46421f.png) [@ananddr](https://community.unix.com/u/ananddr)\
**Post date:** [September 16, 2008, 7:53am UTC](https://community.unix.com/t/displaying-array-elements-in-shell-scripts/212684/3 "2008-09-16T07:53:35Z")

</div>

Hi era ,

thanks for your help!!!!

It works fine after your suggestion....

Thanks a lot....

Anand.
