# i cannot give array declaration in shell script

**URL:** <https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080>\
**Category:** Solaris\
**Created:** [February 11, 2008, 9:11am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080 "2008-02-11T09:11:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 11, 2008, 9:11am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/1 "2008-02-11T09:11:44Z")

</div>

Dear all,  
i m unable to give array decalartion in solaris Operating system shell script

and script was so.sh

declare -a bull[90]

for i in 1 2 3 4 5 6 7 8 9  
do

bull[$i]=$i  
echo "${bull[$i]}"

done

it is throwing an error called

so.sh: declare: not found  
so.sh: bull[1]=1: not found  
so.sh: bad substitution

Sir if u cud help ragrding this issue

---

<div class="post-metadata">

**Author:** ![System\_Shock](https://community.unix.com/user_avatar/community.unix.com/system_shock/32/368_2.png) [@System\_Shock](https://community.unix.com/u/System_Shock)\
**Post date:** [February 11, 2008, 10:12am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/2 "2008-02-11T10:12:56Z")

</div>

For starters, "declare" means nothing to the shell.  
If you are going to be using arrays, I'd suggest you switch to the korn shell, which I find more efficient in handling arrays than any other shell. In korn shell, you set an array with:

```nohighlight
# set -A bull 1 2 3 4 5 6 7 8 9

```

This sets array "bull" with numbers 1 through 9 in spots 0 through 8, respectively.

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [February 11, 2008, 7:08pm UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/3 "2008-02-11T19:08:45Z")

</div>

Actually "declare -a" is supported in the bash shell.

If the bash shell is available on your system, then the following should work (you may need to change "/usr/local/bin/bash" to point to wherever bash is installed on your system)

```nohighlight
#!/usr/local/bin/bash

declare -a bull

for i in 1 2 3 4 5 6 7 8 9
do
    bull[$i]=$i
    echo "${bull[$i]}"
done

```

You can also perform the loop as follows

```nohighlight
for (( i=1; i < 10; i++ ))
do
    bull[$i]=$i
    echo "${bull[$i]}"
done

```

---

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 12, 2008, 6:04am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/4 "2008-02-12T06:04:41Z")

</div>

Sir i m unable to do .. above mentioned in a script ..

It shooting same old error.

ara.sh: declare: not found  
ara.sh: syntax error at line 3: `(' unexpected

ara.sh is

declare -a bull[10]

for (( i=1; i \< 10; i++ ))  
do  
bull[$i]=$i  
echo "${bull[$i]}"  
done

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [February 12, 2008, 6:18am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/5 "2008-02-12T06:18:38Z")

</div>

What shell are you using?

---

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 12, 2008, 7:19am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/6 "2008-02-12T07:19:06Z")

</div>

Sir i m using BASH shell in solaris..

---

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 12, 2008, 9:13am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/7 "2008-02-12T09:13:26Z")

</div>

> [@fpmurphy](#):
>
> What shell are you using?

Sir i m using Bash Shell in which i m unable to work out this code..

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [February 12, 2008, 5:28pm UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/8 "2008-02-12T17:28:37Z")

</div>

What is the output of the following?

```nohighlight
bash --version

```

```nohighlight
whence bash

```

---

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 13, 2008, 1:42am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/9 "2008-02-13T01:42:22Z")

</div>

when i execute

$bash --version

GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)  
Copyright (C) 2004 Free Software Foundation, Inc.

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/5_5575768a8748004e209b776fc1b2916d.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [February 13, 2008, 3:05am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/10 "2008-02-13T03:05:38Z")

</div>

Are you executing this command from the command line or inside your script (which you should) ?

---

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 13, 2008, 5:13am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/11 "2008-02-13T05:13:56Z")

</div>

I m unable to execute it. It is shooting same error.

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/5_5575768a8748004e209b776fc1b2916d.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [February 13, 2008, 12:11pm UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/12 "2008-02-13T12:11:24Z")

</div>

Add the "ptree $$" command in your script before the "declare" command and post its whole output.

---

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 15, 2008, 1:34am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/13 "2008-02-15T01:34:33Z")

</div>

Sir after adding ptree $$ in program...

this is Output:

356 /usr/lib/ssh/sshd  
19148 /usr/lib/ssh/sshd  
19151 /usr/lib/ssh/sshd  
19153 -bash  
19175 sh ara.sh  
19176 ptree 19175  
ara.sh: declare: not found  
ara.sh: syntax error at line 4: `(' unexpected

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/5_5575768a8748004e209b776fc1b2916d.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [February 15, 2008, 2:09am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/14 "2008-02-15T02:09:45Z")

</div>

You are launching your script the wrong way.  
Instead of running "sh ara.sh", change the script to be executable:

```nohighlight
chmod +x ara.sh

```

Then, you'll be able to run it like a regular program

```nohighlight
./ara.sh

```

You should also probably rename it to ara.bash as it is a bash script.

Alternatively, you can just launch the proper interpreter:

```nohighlight
bash ara.sh

```

---

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 15, 2008, 2:18am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/15 "2008-02-15T02:18:09Z")

</div>

Sir, Now i can execute the script ..Thanks a lot for u tip it really worked.

of changing mode to executable ...and Sir i have not renamed the script even though it executed.

---

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 15, 2008, 2:21am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/16 "2008-02-15T02:21:21Z")

</div>

Sir, Now i can execute the script ..Thanks a lot for u tip it really worked.

of changing mode to executable ...and Sir i have not renamed the script even though it executed.

But Sir i have doubt y u have asked ptree $$ (process tree) to put before declare command. Cud explain me about that also.

---

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 15, 2008, 2:44am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/17 "2008-02-15T02:44:45Z")

</div>

Sir, Now i can execute the script ..Thanks a lot for u tip it really worked.

of changing mode to executable ...and Sir i have not renamed the script even though it executed.

But Sir i have doubt y u have asked ptree $$ (process tree) to put before declare command. Cud you explain me about that also.

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/5_5575768a8748004e209b776fc1b2916d.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [February 15, 2008, 5:38am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/18 "2008-02-15T05:38:54Z")

</div>

> [@naree](#):
>
> But Sir i have doubt y u have asked ptree $$ (process tree) to put before declare command. Cud you explain me about that also.

That was intended to help understanding why your script wasn't executed by the expected shell. I.e. for debugging purpose. Now it works, you can remove that line from your script.

---

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 16, 2008, 3:08am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/19 "2008-02-16T03:08:38Z")

</div>

Sir please Elucidate the use of ptree $$ .. in other purposes also.

---

<div class="post-metadata">

**Author:** ![naree](https://community.unix.com/letter_avatar/naree/32/5_5575768a8748004e209b776fc1b2916d.png) [@naree](https://community.unix.com/u/naree)\
**Post date:** [February 19, 2008, 1:17am UTC](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080/20 "2008-02-19T01:17:28Z")

</div>

i have tired this command

find /data -type f -mmin 5 -print

but it is not working

[Next page](https://community.unix.com/t/i-cannot-give-array-declaration-in-shell-script/185080.md?page=2)
