# remove an element from array

**URL:** <https://community.unix.com/t/remove-an-element-from-array/272499>\
**Category:** Shell Programming and Scripting\
**Created:** [August 31, 2010, 4:26am UTC](https://community.unix.com/t/remove-an-element-from-array/272499 "2010-08-31T04:26:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelrozar17](https://community.unix.com/user_avatar/community.unix.com/michaelrozar17/32/1975_2.png) [@michaelrozar17](https://community.unix.com/u/michaelrozar17)\
**Post date:** [August 31, 2010, 4:26am UTC](https://community.unix.com/t/remove-an-element-from-array/272499/1 "2010-08-31T04:26:18Z")

</div>

I need to remove an element from the below array variable TABLENAME.

```nohighlight
#!/bin/ksh
set -A TABLENAME "mirf roxar keke mirs"
echo "the array is ${TABLENAME[*]}"

```

If i need to remove say keke and have the final TABLENAME as below, how this could be achieved. Pls throw some light.

```nohighlight
echo "Modified array is ${TABLENAME[*]}
mirf roxar mirs

```

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [August 31, 2010, 5:11am UTC](https://community.unix.com/t/remove-an-element-from-array/272499/2 "2010-08-31T05:11:55Z")

</div>

The way you declare the array, you only have 1 element in it - you have to leave the double quotation marks away.

There is no functions available to delete elements directly from an array in ksh. You can either set it in a case of a match while looping (I did it without using it's index but treating it as a list) through the elements to "nothing" but you will have a gap in the array then. If you want to remove it and have the array elements filling up the gap, you can try something like this:

```nohighlight
> set -A TABLENAME mirf roxar keke mirs
> echo ${TABLENAME[2]}
keke
> TMP=$(for A in ${TABLENAME[*]}; do echo $A| grep -v keke; done)
> echo $TMP ## at this stage, TMP is no array since it's elements have no index, so we will make an array of it again with set -A 2 lines below:
mirf roxar mirs
> set -A TABLENAME $(echo $TMP)
> echo ${TABLENAME[2]}
mirs

```

---

<div class="post-metadata">

**Author:** ![michaelrozar17](https://community.unix.com/user_avatar/community.unix.com/michaelrozar17/32/1975_2.png) [@michaelrozar17](https://community.unix.com/u/michaelrozar17)\
**Post date:** [August 31, 2010, 6:53am UTC](https://community.unix.com/t/remove-an-element-from-array/272499/3 "2010-08-31T06:53:31Z")

</div>

Thank you. But wot if the element that need to be removed is random or that the index of the element to be removed is unknown.

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [August 31, 2010, 7:27am UTC](https://community.unix.com/t/remove-an-element-from-array/272499/4 "2010-08-31T07:27:58Z")

</div>

If it is a random number from the index, you can identify it.  
If the index is unknown, what do you know then - the string?

I think both can be handled with the example.
