bash

array

声明数组,遍历

allThreads=(1 2 4 8 16 32 64 128)
echo "----------------------------------------simple"
for t in ${allThreads[@]}; do
echo $t
done

output:
----------------------------------------simple
1
2
4
8
16
32
64
128

遍历idx

echo "----------------------------------------idx"
for t in ${!allThreads[@]}; do
echo $t
done

----------------------------------------idx
0
1
2
3
4
5
6
7

数组长度

echo "array length:${!allThreads[@]}"

*和@区别

for t in "${allThreads[@]}"; do
echo "t @ is:$t"
done

for t in "${allThreads[*]}"; do
echo "star t is:$t"
done

echo "@t is|${allThreads[@]}|"
echo "* is|${allThreads[*]}|"
IFS=,; echo "* is|${allThreads[*]}|"

output:
t @ is:1
t @ is:2
t @ is:4
t @ is:8
t @ is:16
t @ is:32
t @ is:64
t @ is:128
star t is:1 2 4 8 16 32 64 128
@t is|1 2 4 8 16 32 64 128|
* is|1 2 4 8 16 32 64 128|
* is|1,2,4,8,16,32,64,128|

Leave a Comment