EN
Bash - create array
3
points
This article, we would like to show you how to create an array in Bash.
There are several ways to create an array in Bash:
- by using array syntax:
#!/bin/bash array=(1 two 'third value' "fourth value")
#!/bin/bash array=( 1 two 'three' "four" )
- by using declare command:
#!/bin/bash declare -a array=(1 two 'third value' "fourth value")
- by assigning single values:
#!/bin/bash array[0]=1 array[1]=two array[2]='third value' array[3]="fourth value"
Note: when we try to assign values to non-existing array the array is created automatically.
Usage example
#!/bin/bash
array=(1 two 'third value' "fourth value")
for i in "${array[@]}"
do
echo "$i"
done
Output:
1
two
third value
fourth value