Size: 737
Comment:
|
← Revision 3 as of 2023-01-29 21:15:40 ⇥
Size: 799
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 11: | Line 11: |
An array can be declared splicitly like: | An array can be declared explicitly with the `declare` [[Bash/BuiltinCommands#Declare|builtin]]: |
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 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[@]}.