Arrays
SH · Bash Scriptingsyntax
arr=(val1 val2 val3)
${arr[0]} ${arr[@]} ${#arr[@]}example
SERVERS=("web01.prod" "web02.prod" "db01.prod")
echo "First: ${SERVERS[0]}"
echo "All: ${SERVERS[@]}"
echo "Count: ${#SERVERS[@]}"
SERVERS+=("cache01.prod")
for srv in "${SERVERS[@]}"; do
ssh deploy@"${srv}" 'uptime'
doneoutput
First: web01.prod
All: web01.prod web02.prod db01.prod
Count: 3Note Indices start at 0. Always quote "${arr[@]}" to preserve elements with spaces. ${#arr[@]} gives the length. += appends. unset 'arr[1]' removes an element (but does not reindex). Bash arrays are not available in sh/POSIX shell.