Bash Shell Script Iterate Through Array Values
Posted by Quinn McHenry in Bourne shell scripting
Having an array of variables is of no use unless you can use those values somehow. This tech-recipe shows a few methods for looping through the values of an array in the bash shell.
Take, for example, the array definition below:
names=( Jennifer Tonya Anna Sadie )
The following expression evaluates into all values of the array:
${names[@]}
It also 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 script 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]}
About Quinn McHenry
View more articles by Quinn McHenry
The Conversation
Follow the reactions below and share your own thoughts.





November 25, 2008 at 6:48 pm, Anonymous said:
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.
February 05, 2009 at 5:03 pm, RobbyC said:
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)
October 01, 2009 at 8:19 am, TuxSax said:
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$//’
July 28, 2011 at 2:30 am, Reginald Pierce said:
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.
February 11, 2009 at 5:32 pm, JamesDS said:
ah, just what I was looking for – perfect, thanks!
February 25, 2009 at 12:02 am, mati said:
if you really have to use windows, then cygwin is your salvation.
February 25, 2009 at 1:38 pm, Olly said:
Thank you very much for this snipped – just what I was looking for!
May 06, 2009 at 6:53 am, kool said:
thnx a lot pal
I owe u 5 marks of my final exam
January 11, 2010 at 7:01 am, Anonymous said:
Thanks a lot..
May 25, 2010 at 5:43 pm, Thanish said:
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
February 17, 2011 at 9:02 pm, minoc said:
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
February 17, 2011 at 9:08 pm, minoc said:
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…
February 19, 2011 at 12:17 am, Jack said:
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
November 06, 2010 at 7:25 pm, annie said:
great!…
i really need this
thanks
February 08, 2011 at 1:36 pm, sriram said:
it works perfectly
Thanks a lot
December 21, 2011 at 7:08 pm, sitami said:
what must I do whene I only want to use the frist element of the array?
February 21, 2012 at 10:25 pm, spike said:
Awesome tips!
April 18, 2012 at 6:06 pm, Kevin said:
Hi, great tip! However, I think the loop needs a semicolon before the word “do”, i.e:
for name in ${names[@]}; do
echo $name
# other stuff on $name
done
December 12, 2012 at 2:48 pm, punit said:
cant stote,
x[08]=”Annthing”
also
x[09]=”Annthing”
Only on this 2 key values it gives error
-bash: 08: value too great for base (error token is “08″)
-bash: 08: value too great for base (error token is “09″)
Any solution ???
January 10, 2013 at 8:54 am, Nils said:
When i use the ${array[@]} format to loop over the elements of the array, the sequence it shows is wrong though the elements are all present!
Any idea why?
May 09, 2010 at 6:36 am, TuxSax said:
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.