⇤ ← Revision 1 as of 2023-01-22 22:24:19
Size: 737
Comment:
|
Size: 743
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 22: | Line 22: |
myarray=("first" "second" "third") | myarray=( "first" "second" "third" ) |
Line 34: | Line 34: |
myarray+="new item" | myarray+=( "new item" ) |
Bash Array
Contents
Declaration
An array can be declared splicitly like:
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[@]}.