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!
На этой странице сайта вы можете посмотреть видео онлайн Shell Scripts Part 5 - Array длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Virtu Mentor 13 Октябрь 2020, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 184 раз и оно понравилось 3 зрителям. Приятного просмотра!