---
title: "bash操作数组"
author: "luogen33"
date: "2011-09-06T23:31:58Z"
tags: []
categories: ["编程语言"]
slug: "bash"
draft: false
lastmod: "2011-09-06T23:31:58Z"
id: 1167156
url: "https://luogen33.iteye.com/blog/1167156"
---

#!/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  
do 
echo ${colors}; 
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; 
echo ${#colors}; 
#remove all items of colors array,there are three methods . 
#unset colors; 
unset colors
; #unset colors; 
echo ${#color};