On Thu, May 1, 2025, 13:52 Greg Wooledge <greg@wooledge.org> wrote:
> On Thu, May 01, 2025 at 08:46:59 -0400, Chet Ramey wrote:
> > You can already remove portions of strings with word expansion operators,
> > and it's fairly easy to write a function to, say, remove the last element
> > of an indexed array.
>
> I'm not saying that anything needs to be changed, but it's actually a
> bit of a pain in the butt to remove the last element of an indexed
> array, because of sparseness. The problem breaks down to "find the
> last index" and then an unset, and the only ways to find the last index
> are to iterate over all the indices, or create a second array containing
> all the indices and then find *its* last element via ${#temp[@]}.
>
Why not use a negative offset?
$ a=([1]=x [3]=y); unset 'a[-1]'; declare -p a
declare -a a=([1]="x")