Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-19-2014, 11:29 PM   #1
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
How to find the index of bash array

How to find the index of the bash array if the index is a string.

declare -A TEST

Here I wont to know what is the index of the first second and third array
Old 01-20-2014, 12:01 AM   #2
Ser Olmy
Senior Member
Registered: Jan 2012
Distribution: Slackware
Posts: 2,651

Rep: Reputation: Disabled
To retrieve all indices, use ${!TEST[@]}. The exclamation mark means you get a list of indices instead of a list of values, which is what ${TEST[@]} would produce. You can use either of these in a for loop:
declare -A TEST


for x in ${!TEST[@]}; do
  echo Index ${x} points to ${TEST[${x}]}
It's important to remember that the ordering of elements in an associate array is not defined. While this array obviously has three index/value pairs, they may not necessarily appear in the order they were created when you iterate through the array.

In other words, you can't ask for the index or the value of the "third" member. You'll have to look for a value corresponding to a certain index.
2 members found this post helpful.
Old 01-20-2014, 12:06 AM   #3
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use a variable to index an array in Bash shell script? anotheremily Programming 3 06-22-2012 09:46 PM
[SOLVED] Compare array index number against element in bash rewtnull Programming 10 11-01-2011 02:53 PM
get index of an element in array in bash mangatmodi Programming 4 11-20-2009 07:45 AM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
C++ find array index for largest number. joshp Programming 7 03-15-2009 10:56 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:33 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration