Bash array operations and examples

Split to array:

while IFS=’;’ read -ra ADDR; do
for i in “${ADDR[@]}”; do
# process “$i”
done
done <<< “$IN”

Define:

distro=(“redhat” “debian” “gentoo”)

Element:


${ArrayName[subscript]}

Length:


echo “${#distro[@]}”

Example

!/bin/bash

define array

name server names FQDN

NAMESERVERS=(“ns1.nixcraft.net.” “ns2.nixcraft.net.” “ns3.nixcraft.net.”)

get length of an array

tLen=${#NAMESERVERS[@]}

use for loop read all nameservers

for (( i=0; i<${tLen}; i++ ));
do
echo ${NAMESERVERS[$i]}
done