Hello Everyone,
Welcome to VIrtuMentor!!
Today, we will learn about an Array’s in shell scripts.
An Array is variable in shell script which hold multiple values with same or different data types.
Normally, in shell script everything considered as string.
First we create a script called array.sh and add shy bang value
#!/bin/bash
There are three ways to declare array.
1. Indirect
2. Explicit
3. Compound
Let’s declare an array.
In Indirect, we declare an array as:
Virtu[0]=“Mentor”
In this Virtu is an array name
0 is Index number and “Mentor” is value of an array
In array the Index start with zero and goes to n-1
means if we have 10 element in array then the index of last element is 9
as index start with zero
In Explicit, we first declare array and then assign values to it
To declare it we use declare keyword
declare -a Virtu
In Compound, we can declare array with values like
Virtu=(Mentor 21 Linux Bash Scripts)
We can also add new values to array later.
In this tutorial, we are using compund array
So lets comment Indirect and Explicit array declared above
Now we will see how to print the value of array
To print all elements of array we use
echo ${Virtu[@]} and
echo ${Virtu[*]}
We can also print selective index value
To print first element
echo ${Virtu[0]}
echo ${Virtu}
To print selected index value
echo ${Virtu[3]}
echo ${Virtu[1]}
We can also print the length of element in array
To print length we use ‘#’
echo ${#Virtu[0]}
This is all about an Array in bash script.
If you like my video, please do like, share and subscribe to VirtuMentor
and comments for any suggestions
Thank you!
En esta página del sitio puede ver el video en línea Shell Scripts Part 5 - Array de Duración hora minuto segunda en buena calidad , que subió el usuario Virtu Mentor 13 octubre 2020, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 184 veces y le gustó 3 a los espectadores. Disfruta viendo!