Get the index of a value in a Bash array

后端 未结 15 515
说谎
说谎 2021-01-30 03:41

I have something in bash like

myArray=(\'red\' \'orange\' \'green\')

And I would like to do something like

echo ${         


        
15条回答
  •  无人及你
    2021-01-30 04:32

    No. You can only index a simple array with an integer in bash. Associative arrays (introduced in bash 4) can be indexed by strings. They don't, however, provided for the type of reverse lookup you are asking for, without a specially constructed associative array.

    $ declare -A myArray
    $ myArray=([red]=0 [orange]=1 [green]=2)
    $ echo ${myArray[green]}
    2
    

提交回复
热议问题