Loop through an array of strings in Bash?

前端 未结 19 3232
情深已故
情深已故 2020-11-22 03:59

I want to write a script that loops through 15 strings (array possibly?) Is that possible?

Something like:

for databaseName in listOfNames
then
  # D         


        
19条回答
  •  眼角桃花
    2020-11-22 04:31

    Simple way :

    arr=("sharlock"  "bomkesh"  "feluda" )  ##declare array
    
    len=${#arr[*]}  # it returns the array length
    
    #iterate with while loop
    i=0
    while [ $i -lt $len ]
    do
        echo ${arr[$i]}
        i=$((i+1))
    done
    
    
    #iterate with for loop
    for i in $arr
    do
      echo $i
    done
    
    #iterate with splice
     echo ${arr[@]:0:3}
    

提交回复
热议问题