-   Programming (
-   -   get index of an element in array in bash (

mangatmodi 11-19-2009 09:23 AM

get index of an element in array in bash
I have an array say

array_name=( XXX YYY ZZZ aaa bbb ccc)
and I want to get Index of aaa. Is there any function or a simple way, other than looping, to get Index of an element of an array.

I don't want to use loop, since it is a part of a larger program and looping will really make it complex

I'm using GNU bash, version 3.2.33(1)-release (i386-redhat-linux-gnu)

ghostdog74 11-19-2009 09:51 AM


echo ${!array_name[@]}

mangatmodi 11-19-2009 10:01 AM


Originally Posted by ghostdog74 (Post 3762713)

echo ${!array_name[@]}


Sir thank you for your reply but I still didn't get it. The above code give me following output

0 1 2 3 4 5
But how can I deduce from it that 3 is the Index of aaa ?

ghostdog74 11-19-2009 07:46 PM

do it loop. Its really not that complex. after you done it, make a function out of it, so that next time, you can use it again

mangatmodi 11-20-2009 08:45 AM

Thank you Sir,

This is second time you are helping me in this week sir.
I had solved the problem in the same manner before your last post though :P

Once again, I'm really grateful to you

All times are GMT -5. The time now is 05:03 AM.