Linux Array & Associative Array

Ordinary Array:

Assign1: arrayName=(value1 value2 ...)

Assign2: arrayName[index]=value

Length: ${#arrayName[*]} or ${#arrayName[@]} 

Indexes: ${!arrayName[*]} or ${#arrayName[@]}

Example:

#!/bin/bash

group1=(rio amos)
group2[0]=bill
group2[1]=frank
group2[2]=bruce

echo group1 has ${#group1[*]} person[s]
echo they are ${group1[*]}
echo group2 has ${#group2[@]} person[s]
echo they are ${group2[@]}

Associative Array:

Define: declare -A arrayName

Assign1: arrayName=([index1]=value1 [index2]=value2 ...)

Assign2: arrayName[index]=value

Length: ${#arrayName[*]} or ${#arrayName[@]}

Indexes: ${!arrayName[*]} or ${#arrayName[@]}

Example:

#!/bin/bash

declare -A team1
declare -A team2

team1=([rio]=1981 [amos]=1990)
team2[frank]=1983
team2[bill]=1984
team2[bruce]=1990

echo team1 has ${#team1[*]} person[s]
echo they are ${!team1[*]}

echo team2 has ${#team2[@]} person[s]
echo they are ${!team2[@]}



本文出自 “Frank” 博客,请务必保留此出处http://zengfanhong.blog.51cto.com/8894077/1569554

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。