Remove the last element from an array

前端 未结 6 1217
甜味超标
甜味超标 2020-12-13 08:55

I want to remove the last entry in my array, and I want the array to show me that it has 1 less entry when I am using the ${#array[@]}. This is the current line

6条回答
  •  醉梦人生
    2020-12-13 09:19

    The following works fine for Mac/bash@3.x and Linux (ubuntu/bash@4.x)

    unset arr[$[${#arr[@]}-1]] # non-sparse array only
    

    in more details:

    len=${#arr[@]}
    idx=$[$len-1]    # <=> $(($len-1))
    unset arr[$idx]
    

提交回复
热议问题