On Sat, Oct 15, 2016 at 11:12 AM, Marco Ippolito <maroloc...@gmail.com> wrote: > Bash has elegant and powerful constructs like `mapfile', > yet it is missing something as easy as an array "pop". > > Extract the last value of an array at the same time as > removing it from the array. > > Is this the best one can do? > > $ a=(1 2 3); v=${a[-1]}; unset 'a[-1]'; printf '%s\n' "$v" "${a[@]}" > > The 2-step approach gets tiresome after a while.
Does this help? pop() { local -n _a=$1 printf -v "$2" "${_a[-1]}" unset _a[-1] } declare -a a=(a b c) while ((${#a[@]})); do pop a v declare -p a v done