Bash Arrays

BASH Arrays

See here.

Indexed arrays

1names[0]=Yoesf
2names[1]=David
3echo $names        # will show: Yosef
4echo ${names[1]}   # will show: David

Assigning complete arrays:

1-> girlz=(Betty Liza Jane)
2-> echo ${girlz[2]}
3Jane
4-> echo ${girlz}
5Betty
6-> 

Associative Arrays

You have to use declare -A name before:

 1-> declare -A menheight
 2-> menheight[Dave]=195
 3-> menheight[Sean]=170
 4-> menheight[Bob]=180
 5-> echo ${menheight[Dave]}
 6195
 7-> echo ${menheight[Sean]}
 8170
 9-> echo ${menheight[Bob]}
10180
11-> 

Accessing keys and Values

Use "!" to get values:

1-> echo ${menheight[@]}
2195 170 180
3-> echo "${!menheight[@]}"
4Dave Sean Bob
5-> 

Loop over elements:

 1-> for key in ${menheight[@]}; do printf "%s %s\n" $key ${menheight[$key]}; done
 2195 
 3170 
 4180 
 5-> 
 6-> 
 7-> 
 8-> for key in "${!menheight[@]}"; do printf "%s %s\n" $key ${menheight[$key]}; done
 9Dave 195
10Sean 170
11Bob 180
12->