βArraysβ in POSIX
POSIX Shell doesnβt have arrays but it does have βan arrayββ¦ kind of.
You can use the tools for managing arguments in semi-familiar ways to how arrays are managed in BASH. Each function has itβs own arguments too so each one has itβs own βarrayβ.
Play in terminal:
# Set arguments (do this first if playing in terminal)
set Setting 5 new array "elements here"
# Append an argument
set -- "$@" "and 1 more"
# Print number of arguments
echo $#
# Print all arguments (argument spaces and separator spaces are separate)
echo $@
# Print all arguments (spaces are ambiguous)
echo $*
# Print the 2nd argument
# Note: After $9 braces should be used to assure cross shell compatibility, ie: ${10}
echo $2
# Remove 2 arguments from the beginning
shift 2
# Remove 2 arguments from the end using rev
set -- $(rev <<< "$@")
shift 2
set -- $(rev <<< "$@")
# Remove 2 arguments from the end cycling through a loop
for i in $(seq 1 $#); do
[ "$i" -le $(($# - 2)) ] && set -- "$@" "$1"
shift
done
# Remove all arguements
shift $#
# Loop through all arguments
for Arg do
echo "$Arg"
done