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:
or:
xxxxxxxxxx
12
3array=(1 two 'third value' "fourth value")
xxxxxxxxxx
12
3array=(
41
5two
6'three'
7"four"
8)
- by using declare command:
xxxxxxxxxx
12
3declare -a array=(1 two 'third value' "fourth value")
- by assigning single values:
xxxxxxxxxx
12
3array[0]=1
4array[1]=two
5array[2]='third value'
6array[3]="fourth value"
Note: when we try to assign values to non-existing array the array is created automatically.
xxxxxxxxxx
1
2
3
array=(1 two 'third value' "fourth value")
4
5
for i in "${array[@]}"
6
do
7
echo "$i"
8
done
Output:
xxxxxxxxxx
1
1
2
two
3
third value
4
fourth value