Bash Array


Declaration

An array can be declared explicitly with the declare builtin:

declare -a myarray

An array is also declared implicitly by any of:

myarray[0]="item in first index of new array"

myarray=( "first" "second" "third" )


Usage

To append a new value to an existing array, try:

myarray+=( "new item" )

To access an item from an array, try:

first="${myarray[0]}"
last="${myarray[-1]}"

To access a slice of an array, try:

all="${myarray[@]}"
all_but_first="${myarray[@]:1}"
first_three="${myarray[@]::3}"

The length of an array is available with ${#myarray[@]}.


CategoryRicottone

Bash/Array (last edited 2023-01-29 21:15:40 by DominicRicottone)