Arrays in unix shell?

2019-01-04 23:44发布

How do I create an array in unix shell scripting?

14条回答
戒情不戒烟
2楼-- · 2019-01-05 00:40

You can try of the following type :

#!/bin/bash
 declare -a arr

 i=0
 j=0

  for dir in $(find /home/rmajeti/programs -type d)
   do
        arr[i]=$dir
        i=$((i+1))
   done


  while [ $j -lt $i ]
  do
        echo ${arr[$j]}
        j=$((j+1))
  done
查看更多
家丑人穷心不美
3楼-- · 2019-01-05 00:44

There are multiple ways to create an array in shell.

ARR[0]="ABC"
ARR[1]="BCD"
echo ${ARR[*]}

${ARR[*]} prints all elements in the array.

Second way is:

ARR=("A" "B" "C" "D" 5 7 "J")
echo ${#ARR[@]}
echo ${ARR[0]}

${#ARR[@]} is used to count length of the array.

查看更多
登录 后发表回答