bash 数组说明

Posted on Posted in bash
文章目录[隐藏]

bash 数组说明

参数

  • arr=() 创建一个空的数组
  • arr=(1 2 3) 初始化数组
  • ${arr[2]} 数组第三项
  • ${arr[@]} 数组所有项
  • ${!arr[@]} 数组下标
  • ${#arr[@]} 数组长度
  • arr[0]=3 修改数组第一项
  • arr+=(4) 追加数组
  • str=$(ls) 返回一个字符串
  • arr=( $(ls) ) 返回一个数组
  • ${arr[@]:s:n} 返回下标ss+n的值,包括下标s的值,但不包括s+n的值

例子

#!/bin/bash
allThreads=(1 2 4 8 16 32 64 128)
echo ${allThreads[1]}
echo ${!allThreads[@]}
echo ${#allThreads[@]}
echo ${allThreads[@]:3:2}

for t in ${allThreads[@]}; do
    echo --threads $t
done

for i in ${!allThreads[@]}; do
    echo --threads ${allThreads[$i]}
done

allRuntimes=()
for j in ${allThreads[@]}; do
    allRuntimes+=( $j)
done

for k in {1..10};
do
    echo $k
done

echo $(ls)
arrE=( $(ls) )
for l in ${arrE[@]}; do
    echo $l
done
» 转载请注明来源:若我若鱼 » bash 数组说明

Leave a Reply

Your email address will not be published. Required fields are marked *

ten − 5 =