Стек Stack<T> (LIFO)

Массивы и коллекции

#bash has no stack: an array, where push adds to the end
#and pop takes from the end
intStack=()
intStack+=(1)
intStack+=(3)
intStack+=(5)

#the index -1 is the last element (bash 4.2 and newer)
top=${intStack[-1]}
#top is 5

#pop: read the last element, then remove it;
#the quotes keep [-1] from being read as a file name pattern
first=${intStack[-1]}
unset 'intStack[-1]'
#first is 5
second=${intStack[-1]}
unset 'intStack[-1]'
#second is 3
third=${intStack[-1]}
unset 'intStack[-1]'
#third is 1

#a pop function cannot be called as $(pop): $( ) runs it
#in a subshell, and the stack there is a copy

((${#intStack[@]} == 0)) && isEmpty=true || isEmpty=false
#isEmpty is true

echo "top is $top"
echo "first is $first"
echo "second is $second"
echo "third is $third"
echo "isEmpty is $isEmpty"