Linux循环输出数组值,linux shell 数组的长度计算、修改、循环输出等操作

在shell中,数组变量的复制有两种方法:

(1) name = (value1 ... valuen)此时下标从0开始

(2) name[index] = value

example:

#1/bin/sh

#arrayTest

name=(yunix yhx yfj)

echo "array is:${name[@]}"

echo "array length is:${#name[*]}"

echo ${name[1]}

name[1]=yang

echo ${name[1]}

read -a name

echo ${name[1]}

echo "loop the array"

len=${#name[*]}

i=0

while [ $i -lt $len ]

do

echo ${name[$i]}

let i++

done

result:

array is:yunix yhx yfj

array length is:3

yhx

yang

a b c d e

b

loop the array

a

b

c

d

e

下面的是关于数组的输出实例

example:

#!/bin/sh

#arrayLoopOut

read -a array

len=${#array[*]}

echo "array's length is $len"

echo "use while out the array:"

i=0

while [ $i -lt $len ]

do

echo -n "${array[$i]}"

let i++

done

echo

echo "use for out the array:"

for ((j=0;j

result:

a b c d e f g

array's length is 7

use while out the array:

abcdefg

use for out the array:

abcdefg

use for in out the array:

abcdefg