Get the index of a value in a Bash array

后端 未结 15 508
说谎
说谎 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:22

    myArray=('red' 'orange' 'green')
    echo ${myArray[@]}
    arrayElementToBeRemoved='orange'
    echo "removing element: $arrayElementToBeRemoved"
    # Find index of the array element (to be kept or preserved)
    let "index=(`echo ${myArray[@]} | tr -s " " "\n" | grep -n "$arrayElementToBeRemoved" | cut -d":" -f 1`)-1"
    unset "myArray[$index]"
    echo ${myArray[@]}
    

提交回复
热议问题