# Bash shell script undefined array item value question

**URL:** <https://community.unix.com/t/bash-shell-script-undefined-array-item-value-question/336788>\
**Category:** Shell Programming and Scripting\
**Created:** [November 19, 2013, 4:30am UTC](https://community.unix.com/t/bash-shell-script-undefined-array-item-value-question/336788 "2013-11-19T04:30:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lingjing](https://community.unix.com/letter_avatar/lingjing/32/5_5575768a8748004e209b776fc1b2916d.png) [@lingjing](https://community.unix.com/u/lingjing)\
**Post date:** [November 19, 2013, 4:30am UTC](https://community.unix.com/t/bash-shell-script-undefined-array-item-value-question/336788/1 "2013-11-19T04:30:25Z")

</div>

Hello,

I'm new here. I test these expressions's value in my script :  
(in centOS 6 )

```nohighlight
#!/bin/bash
array['test']='something'

echo "############"
echo ${array["test1"]}
echo ${array["test2"]}
echo ${array["5"]}
echo "############"

```

The output result is :

```nohighlight
#################
something
something

#################

```

Why the value of ${array["test1"]} and ${array["test2"]} is "something" instead of empty ?  
Because they are not defined just as ${array["5"]}, however the value of ${array["5"]} is empty and it's correct.

Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![hergp](https://community.unix.com/letter_avatar/hergp/32/5_5575768a8748004e209b776fc1b2916d.png) [@hergp](https://community.unix.com/u/hergp)\
**Post date:** [November 19, 2013, 5:35am UTC](https://community.unix.com/t/bash-shell-script-undefined-array-item-value-question/336788/2 "2013-11-19T05:35:28Z")

</div>

bash does not support associative arrays. You can only use integers as indexes.

---

<div class="post-metadata">

**Author:** ![daPeach](https://community.unix.com/letter_avatar/dapeach/32/5_5575768a8748004e209b776fc1b2916d.png) [@daPeach](https://community.unix.com/u/daPeach)\
**Post date:** [November 19, 2013, 5:52am UTC](https://community.unix.com/t/bash-shell-script-undefined-array-item-value-question/336788/3 "2013-11-19T05:52:45Z")

</div>

bash do support associative arrays.  
they only need to be declared:

```nohighlight
declare -A assoAr
assoAr[subscript]="any value"
assoAr[aosubscript]="another value"
printf '%s\n' "${assoAr[@]}"
any value
another value

```

---

<div class="post-metadata">

**Author:** ![hergp](https://community.unix.com/letter_avatar/hergp/32/5_5575768a8748004e209b776fc1b2916d.png) [@hergp](https://community.unix.com/u/hergp)\
**Post date:** [November 19, 2013, 6:59am UTC](https://community.unix.com/t/bash-shell-script-undefined-array-item-value-question/336788/4 "2013-11-19T06:59:02Z")

</div>

I stand corrected. When was this feature intoduced?

---

<div class="post-metadata">

**Author:** ![daPeach](https://community.unix.com/letter_avatar/dapeach/32/5_5575768a8748004e209b776fc1b2916d.png) [@daPeach](https://community.unix.com/u/daPeach)\
**Post date:** [November 19, 2013, 7:43am UTC](https://community.unix.com/t/bash-shell-script-undefined-array-item-value-question/336788/5 "2013-11-19T07:43:12Z")

</div>

> [@](#):
>
> When was this feature intoduced?

bash 4

---

<div class="post-metadata">

**Author:** ![lingjing](https://community.unix.com/letter_avatar/lingjing/32/5_5575768a8748004e209b776fc1b2916d.png) [@lingjing](https://community.unix.com/u/lingjing)\
**Post date:** [November 19, 2013, 7:43am UTC](https://community.unix.com/t/bash-shell-script-undefined-array-item-value-question/336788/6 "2013-11-19T07:43:29Z")

</div>

Thanks ! after add the declaration  
declare -A array

It works as expected : The undefined item of array has empty value.
