Bash: whole array except the last element
Bash has a neat way of providing all the elements in an array except the first:
"${a[@]:1}"
To get everything but the last one I found:
"${a[@]:0:$((${#a[@]}-1))}"
But, man, this is ugly.
Is there an elegant alternative?
+3
Ole tange
source
to share
1 answer
I'm not sure how much better it would be, but you can drop the arithmetic operator ( $(())
) and the 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 syntactic; the idea remains the same.
+5
heemayl
source
to share