Variable as bash array index?

前端 未结 2 633
执笔经年
执笔经年 2021-02-02 09:43
#!/bin/bash

set -x

array_counter=0
array_value=1

array=(0 0 0)

for number in ${array[@]}
do
    array[$array_counter]=\"$array_value\"
    array_counter=$(($array_co         


        
相关标签:
2条回答
  • 2021-02-02 10:15

    Bash seems perfectly happy with variables as array indexes:

    $ array=(a b c)
    $ arrayindex=2
    $ echo ${array[$arrayindex]}
    c
    $ array[$arrayindex]=MONKEY
    $ echo ${array[$arrayindex]}
    MONKEY
    
    0 讨论(0)
  • 2021-02-02 10:15

    Your example actually works.

    echo ${array[@]}
    

    confirms this.

    You might try more efficient way of incrementing your index:

    ((array_counter++))
    
    0 讨论(0)
提交回复
热议问题