List Assignment
If you are familiar with Perl, C, or Java, you might think that Bash would use commas to separate array elements, however this is not the case; instead, Bash uses spaces:
# Array in Perl
my @array = (1, 2, 3, 4);
# Array in Bash
array=(1 2 3 4)
Create an array with new elements:
array=('first element' 'second element' 'third element')
Subscript Assignment
Create an array with explicit element indices:
array=([3]='fourth element' [4]='fifth element')
Assignment by index
array[0]='first element'
array[1]='second element'
Assignment by name (associative array)
declare -A array
array[first]='First element'
array[second]='Second element'
Dynamic Assignment
Create an array from the output of other command, for example use seq to get a range from 1 to 10:
array=(`seq 1 10`)
Assignment from script's input arguments:
array=("$@")
Assignment within loops:
while read -r; do
#array+=("$REPLY") # Array append
array[$i]="$REPLY" # Assignment by index
let i++ # Increment index
done < <(seq 1 10) # command substitution
echo ${array[@]} # output: 1 2 3 4 5 6 7 8 9 10
where $REPLY
is always the current input