parameter-expansion

Bash negative array index

References the highest assigned index of a nonempty indexed array; negative offsets are relative to one beyond its maximum index.

Formal Syntax & Structure

bash syntaxbash
${name[-1]}

Return Value

string

References the highest assigned index of a nonempty indexed array; negative offsets are relative to one beyond its maximum index.

Working Example

Example Snippetbash
items=([2]='' [7]=last)printf 'count=%s\nlast=%s\nempty=[%s]\n' "${#items[@]}" "${items[-1]}" "${items[2]}"
Bash Reference