Bash: all of array except last element

前端 未结 1 497
眼角桃花
眼角桃花 2021-01-02 03:30

Bash has a neat way of giving all elements in an array except the first:

\"${a[@]:1}\"           

To get all except the last I have found:<

相关标签:
1条回答
  • 2021-01-02 03:58

    I am not sure how much improvement it would be, but you can drop the arithmetic operator ($(())) and starting index (0 here):

    ${a[@]::${#a[@]}-1}
    

    So:

    $ foo=( 1 2 3 )
    
    $ echo "${foo[@]::${#foo[@]}-1}"
    1 2
    

    As you can see, the improvement is purely syntactical; the idea remains the same.

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