bash操作数组

#!/bin/bash
declare -a colors;
echo "Enter your favorite colors (septated from each other by space).";
read -a colors;
#get the length of array
element_count=${#colors[@]};
index=0;

#all over the array.
echo "------all over the array------";

#the No1. method.
echo "---the No1. method ----";
while [ "$index" -lt "$element_count" ]
do
echo ${colors[$index]};
let "index = $index + 1";
done

#the No2 method.
echo "----the No2 method----";
for item in ${colors[@]}
do
echo $item;
done


#remove items of array
echo "-----remove items of array-----";
#remove the first item of colors array.
unset colors[1];
echo ${#colors[@]};
#remove all items of colors array,there are three methods .
#unset colors;
unset colors
; #unset colors[@];
echo ${#color[@]};

你可能感兴趣的:(bash)