35 lines
629 B
Bash
35 lines
629 B
Bash
|
|
#!/bin/bash
|
||
|
|
|
||
|
|
#-------------------------------------------------------------------------------
|
||
|
|
# Example 1 for Bash Tips show 17: Negative indices
|
||
|
|
#-------------------------------------------------------------------------------
|
||
|
|
|
||
|
|
#
|
||
|
|
# Seed the Fibonacci sequence in an indexed array
|
||
|
|
#
|
||
|
|
declare -a fib=(0 1 1)
|
||
|
|
|
||
|
|
#
|
||
|
|
# Populate the rest up to (and including) the 20th element
|
||
|
|
#
|
||
|
|
for ((i = 3; i <= 20; i++)); do
|
||
|
|
fib[$i]=$((fib[i-2]+fib[i-1]))
|
||
|
|
done
|
||
|
|
|
||
|
|
#
|
||
|
|
# Show the whole array
|
||
|
|
#
|
||
|
|
echo "Fibonacci sequence"
|
||
|
|
echo "${fib[*]}"
|
||
|
|
echo
|
||
|
|
|
||
|
|
#
|
||
|
|
# Print a few elements working backwards
|
||
|
|
#
|
||
|
|
for i in {-1..-4}; do
|
||
|
|
echo "fib[$i] = ${fib[$i]}"
|
||
|
|
done
|
||
|
|
|
||
|
|
exit
|
||
|
|
|