LDP/LDP/guide/docbook/abs-guide/neg-array.sh

41 lines
1.0 KiB
Bash

#!/bin/bash
# neg-array.sh
# Requires Bash, version -ge 4.2.
array=( zero one two three four five ) # Six-element array.
# 0 1 2 3 4 5
# -6 -5 -4 -3 -2 -1
# Negative array indices now permitted.
echo ${array[-1]} # five
echo ${array[-2]} # four
# ...
echo ${array[-6]} # zero
# Negative array indices count backward from the last element+1.
# But, you cannot index past the beginning of the array.
echo ${array[-7]} # array: bad array subscript
# So, what is this new feature good for?
echo "The last element in the array is "${array[-1]}""
# Which is quite a bit more straightforward than:
echo "The last element in the array is "${array[${#array[*]}-1]}""
echo
# And ...
index=0
let "neg_element_count = 0 - ${#array[*]}"
# Number of elements, converted to a negative number.
while [ $index -gt $neg_element_count ]; do
((index--)); echo -n "${array[index]} "
done # Lists the elements in the array, backwards.
# We have just simulated the "tac" command on this array.
echo
# See also neg-offset.sh.