bash shell script iterate through array values

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: Bourne shell scripting  

Having an array of variables is of no use unless you can use those values somehow. This recipe shows a few methods for looping through the values of an array in the bash shell.


Given the array definition:

names=( Jennifer Tonya Anna Sadie )

The following expression evaluates into all values of the array:

${names[@]}

and can be used anywhere a variable or string can be used.

A simple for loop can iterate through this array one value at a time:

for name in ${names[@]}
do
echo $name
# other stuff on $name
done

This scrip will loop through the array values and print them out, one per line. Additional statements can be placed within the loop body to take further action, such as modifying each file in an array of filenames.

Sometimes it is useful to loop through an array and know the numeric index of the array you are using (for example, so that you can reference another array with the same index). The same loop in the example above can be achieved this way, too:

for (( i = 0 ; i < ${#names[@]} ; i++ ))
do
echo ${names[$i]}
# yadda yadda
done

In this example, the value ${#names[@]} evaluates into the number of elements in the array (4 in this case). The individual elements of the array are accessed, one at a time, using the index integer $i as ${names[$i]}

 

17 Comments -


  1. Anonymous said on November 25, 2008

    all kinds of errors will popup if you write this (or any) script on windows. i was almost giving up when i realised i had written it on notepad and saved on the linux box over samba. use dostounix to fix the format.

  2. RobbyC said on February 5, 2009

    The reason for this error is that the control character sequence used by DOS (Windows, rn) is different from the sequnce used by UNIX (*NIX, n)

  3. JamesDS said on February 11, 2009

    ah, just what I was looking for – perfect, thanks!

  4. mati said on February 25, 2009

    if you really have to use windows, then cygwin is your salvation.

  5. Olly said on February 25, 2009

    Thank you very much for this snipped – just what I was looking for!

  6. kool said on May 6, 2009

    thnx a lot pal
    I owe u 5 marks of my final exam
    :)

  7. TuxSax said on October 1, 2009

    I had a similar problem until I realized that the file I was working on was created on Windows too.
    I fixed it with:
    sed -i ’s/r$//’

  8. Anonymous said on January 11, 2010

    Thanks a lot..

  9. TuxSax said on May 9, 2010

    Right, I’ve forgot to mention that option too
    If you are working on Ubuntu, like me, then the package “tofrodos” contains the dos2unix utility.

  10. Thanish said on May 25, 2010

    Thanks for the for function m8, you just saved me some hours.
    Now I can get back to my Script. You guys can also place the ‘do’ on the first line
    IMHO it looks better this way.

    for name in ${names[@]}; do

  11. annie said on November 6, 2010

    great!…
    i really need this
    thanks

  12. sriram said on February 8, 2011

    it works perfectly

    Thanks a lot

  13. minoc said on February 17, 2011

    as long as names are not separated by IFS (defaulting to white space).. for example

    names=( “foo bar” “ben smith” )
    for name in ${names[@]}; do echo “[${name}]“; done
    [foo]
    [bar]
    [ben]
    [smith]

    whereas this preserves the spacing in array elements:

    for ((i=0; i

  14. minoc said on February 17, 2011

    Oops., The easiest way to iterate is like you said initially., except you forgot the quotes:

    for name in “${names[@]}”; do

    works., and preserves the internal spaces in the elements…

  15. Jack said on February 19, 2011

    Iterate through a sparse array by using:

    ARR=([0]=”zero” [1]=”one” [10]=”ten”)
    for INDEX in ${!ARR[@]}
    do
    echo index $INDEX “=>” ${ARR[$INDEX]}
    done

  16. Reginald Pierce said on July 28, 2011

    I have found Notepad++ to be a great tool for Windows since it allows you to convert the line endings to Windows, Mac, or Linux while editing the file.  It also has good code highlighting.

  17. sitami said on December 21, 2011

    what must I do whene I only want to use the frist element of the array?

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -