How to do bash script iterator particularly lists in array or variable

I need to traverse lists stored in both bash shell variable and bash array. How do I iterator in bash script? What is the syntax?

Define the shell variable:

var_a="foo bar test"

Use bash for loop:

for v in $var_a
do
	echo "\$var_a = $v"
done

Bash array example

Define the bash array named array_b:

array_b=('one' 'two' 'three')
array_c=(10 20 30 40 50)

Again use bash for loop:

for a in "${array_b[@]}"
do
	echo "\$array_b = $a"
done
# deal with array_c
for a in "${array_c[@]}"
do
	echo "\$array_c = $a"
done

See bash for loop with array elements for more info.

A note about bash $IFS

Use bash variable called $IFS to control variable (default is blank space)

domain_names="cyberciti.biz|google.com|nixcraft.com"
IFS="|"
for d in $domain_names
do
	echo -n "$d : "
        whois "$d" | grep -Ei 'expiry date'
done


See my page for more examples:


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki