Bash Iterate Array Examples

by Vivek Gite on January 27, 2009 · 0 comments

How do I iterate through an array under Bash scripting?

The Bash shell support one-dimensional array variables. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Arrays are indexed using integers and are zero-based.

Creating an array

An array is created automatically if any variable is assigned to using the syntax:

array=( value1 value2 )
users=( www vivek ftp chroot )
ns=( 192.168.1.10 192.168.1.12 )

OR

var[subscript]=value

Accessing subscript

The subscript is treated as an arithmetic expression that must evaluate to a number greater than or equal to zero. You can display values using the following syntax:

echo "${var[0]}"
echo "${ns[0]}"

For Loop Example - Bash Iterate Array

The syntax is as follows:

 
for var in "${ArrayName[@]}"
do
  echo "${var}"
  # do something on $var
done

Create a shell script as follows:

#!/bin/bash
# define file array
files=(/etc/*.conf)
 
# find total number of files in an array
echo "Total files in array : ${#files[*]}"
total=${#files[*]}
# Print 1st file name
echo "First filename: ${files[0]}"
echo "Second filename: ${files[1]}"
echo "Third filename: ${files[1]}"
# total - 1 = last item (subscript) in an array
echo "Last filename: ${files[$(( $total-1 ))]}"
 
echo
echo "****************"
echo "*** For Loop ***"
echo "****************"
# Use for loop iterate through an array
# $f stores current value
for f in "${files[@]}"
do
	echo -n "$f "
done
 
echo
echo
echo "**************************"
echo "*** C Style For Loop ****"
echo "**************************"
echo
# Use c style for loop
# get total subscripts in an array
total=${#files[*]}
#
for (( i=0; i<=$(( $total -1 )); i++ ))
do
    echo -n "${files[$i]} "
done
 
echo
 

Sample outputs:

Total files in array : 77
First filename: /etc/aatv.conf
Second filename: /etc/adduser.conf
Third filename: /etc/adduser.conf
Last filename: /etc/xorg.conf
****************
*** For Loop ***
****************
/etc/aatv.conf /etc/adduser.conf /etc/apg.conf /etc/argus.conf /etc/atool.conf /etc/brltty.conf /etc/ca-certificates.conf /etc/chkrootkit.conf /etc/cowpoke.conf /etc/cvs-cron.conf /etc/cvs-pserver.conf /etc/dconf.conf /etc/dconf-custom.conf /etc/debconf.conf /etc/deluser.conf /etc/devscripts.conf /etc/discover.conf /etc/dnsmasq.conf /etc/dnsproxy.conf /etc/e2fsck.conf /etc/fdmount.conf /etc/ffserver.conf /etc/fuse.conf /etc/gai.conf /etc/gssapi_mech.conf /etc/hdparm.conf /etc/hesiod.conf /etc/host.conf /etc/idmapd.conf /etc/inetd.conf /etc/kernel-img.conf /etc/kernel-pkg.conf /etc/krb5.conf /etc/ld.so.conf /etc/lftp.conf /etc/libao.conf /etc/libuser.conf /etc/logrotate.conf /etc/ltrace.conf /etc/mke2fs.conf /etc/mplayerplug-in.conf /etc/mtools.conf /etc/multitail.conf /etc/netscsid.conf /etc/nscd.conf /etc/nsswitch.conf /etc/ntp.conf /etc/pam.conf /etc/pnm2ppa.conf /etc/popularity-contest.conf /etc/pub.rsnapshot.conf /etc/ra.conf /etc/resolv.conf /etc/rsnapshot.bull.conf /etc/rsnapshot.conf /etc/rsnapshot.rose.conf /etc/rsnapshot.txvip.conf /etc/rssh.conf /etc/scrollkeeper.conf /etc/sensors3.conf /etc/sensors.conf /etc/smartd.conf /etc/sysctl.conf /etc/sysfs.conf /etc/syslog.conf /etc/ts.conf /etc/ucf.conf /etc/uniconf.conf /etc/updatedb.conf /etc/usplash.conf /etc/vnc.conf /etc/vsftpd.conf /etc/warnquota.conf /etc/wodim.conf /etc/wpa_supplicant.conf /etc/wvdial.conf /etc/xorg.conf
**************************
*** C Style For Loop ****
**************************
/etc/aatv.conf /etc/adduser.conf /etc/apg.conf /etc/argus.conf /etc/atool.conf /etc/brltty.conf /etc/ca-certificates.conf /etc/chkrootkit.conf /etc/cowpoke.conf /etc/cvs-cron.conf /etc/cvs-pserver.conf /etc/dconf.conf /etc/dconf-custom.conf /etc/debconf.conf /etc/deluser.conf /etc/devscripts.conf /etc/discover.conf /etc/dnsmasq.conf /etc/dnsproxy.conf /etc/e2fsck.conf /etc/fdmount.conf /etc/ffserver.conf /etc/fuse.conf /etc/gai.conf /etc/gssapi_mech.conf /etc/hdparm.conf /etc/hesiod.conf /etc/host.conf /etc/idmapd.conf /etc/inetd.conf /etc/kernel-img.conf /etc/kernel-pkg.conf /etc/krb5.conf /etc/ld.so.conf /etc/lftp.conf /etc/libao.conf /etc/libuser.conf /etc/logrotate.conf /etc/ltrace.conf /etc/mke2fs.conf /etc/mplayerplug-in.conf /etc/mtools.conf /etc/multitail.conf /etc/netscsid.conf /etc/nscd.conf /etc/nsswitch.conf /etc/ntp.conf /etc/pam.conf /etc/pnm2ppa.conf /etc/popularity-contest.conf /etc/pub.rsnapshot.conf /etc/ra.conf /etc/resolv.conf /etc/rsnapshot.bull.conf /etc/rsnapshot.conf /etc/rsnapshot.rose.conf /etc/rsnapshot.txvip.conf /etc/rssh.conf /etc/scrollkeeper.conf /etc/sensors3.conf /etc/sensors.conf /etc/smartd.conf /etc/sysctl.conf /etc/sysfs.conf /etc/syslog.conf /etc/ts.conf /etc/ucf.conf /etc/uniconf.conf /etc/updatedb.conf /etc/usplash.conf /etc/vnc.conf /etc/vsftpd.conf /etc/warnquota.conf /etc/wodim.conf /etc/wpa_supplicant.conf /etc/wvdial.conf /etc/xorg.conf

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 2 + 14 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: