BASH/C2/Array-Operations-in-BASH/English
Title of script: Array Operations In BASH
Author: Lavitha Pereira
Keywords: Video tutorial, Bash shell, Array
|
|
Display Slide 1 | Welcome to the spoken tutorial on
Array operations in BASH |
Display Slide 2
Learning Objectives |
In this tutorial, we will learn how to
|
Display Slide 3
Learning Objectives (contd.) |
* To find length of an Array and its nth element
|
Display Slide 4
Prerequisites
|
To follow this tutorial,
You should be familiar with the Linux Operating System.
|
Display Slide 5
System Requirements |
For this tutorial I am using
GNU Bash version 4 or above is recommended to practise this tutorial. |
Display Slide 6
Array Definition |
Let us start with the definition of an Array and its characteristics.
|
Display Slide 7
Array Charactristics
|
* There is no maximum limit for the size of an array
|
Display Slide 8
Declare and assign values to an Array Syntax: declare -a arrayname |
We will now see how to declare and assign a value to an Array.
The syntax to declare an Array is - declare space hyphen `a` space arrayname |
“declare” keyword is used to declare an Array.
It is a built-in command in Bash. | |
Display slide 8
Name[index]='value' |
The syntax to assign a value to an Array is-
Name opening square bracket index closing square bracket equal to within single quotes value. |
=== Display Slide 9 ===
ContentsInitialize an Array during declaration |
Now let see how to initialize an Array during declaration.
|
=== Display Slide 10 ===
Initialize an Array during declarationSyntax: declare -a arrayname=('element1' 'element2' 'element3')
|
The syntax is as shown
declare space hyphen `a` space arrayname equal-to opening round bracket within single quotes 'element1' space 'element2' space 'element3' closing round bracket. |
Let us try an example. | |
Press Ctrl+Alt+T | Open the terminal by pressingCtrl+Alt+T keys simultaneously. |
On Terminal >>Type gedit array.sh
|
Type:
gedit space array.sh on the terminal. Now type the code as shown here into your array.sh file. |
Highlight
declare -a Linux=('Debian' 'Redhat' 'Ubuntu' 'Fedora') |
This line declares an Array named Linux with elements -
|
Here
hyphen `a` is a flag. It allows us to read and assign values to an Array. | |
Let us switch back to the slides. | |
=== Display Slide ===
Length of an Array and of its nth element.${#arrayname[@]} |
The length of an Array can be obtained by this code:
Dollar symbol opening curly bracket hash arrayname opening square bracket At sign closing square bracket closing curly bracket |
=== Display Slide (contd.) ===
${#arrayname[n]} |
The length of the nth element can be obtained by this code:
Dollar symbol opening curly bracket hash arrayname opening square bracket index number `n` closing square bracket closing curly bracket.
|
=== Display Slide ===
Print the whole ArraySyntax: ${arrayname[@]}
|
All the elements of the Array can be printed using this syntax.
|
Come back to the text editor. | |
[Highlight]
echo -e "Total number elements in an array Linux: ${#Linux[@]} \n"
|
This line will display total number of elements in the Array Linux.
|
echo -e "The elements of array Linux are: ${Linux[@]} \n" | Next line displays all the elements of the Array Linux. |
echo -e "Third element in array Linux is: ${Linux[2]} \n"
|
This line displays the 3rd element of the Array Linux.
|
echo -e "Length of third element is: ${#Linux[2]} \n" | Lastly, this line displays the number of characters present in the 3rd element. |
Switch to Terminal>>Type chmod +x array.sh>> Press Enter>>
|
Switch to the Terminal.
Let's first make the file executable by typingchmod space plus x space array.sh Press Enter. |
Type ./array>> Press Enter | Now execute by typing
dot slash array.sh Press Enter. |
Output:
[Highlight] Length of the Array Linux is: 4 |
The number of elements or the length of the Array `Linux`is four. |
Output:
[Highlight] The elements of Array Linux are: Debian Redhat Ubuntu Fedora |
The elements of the Array Linux are Debian, Redhat, Ubuntu, Fedora |
Output:
[Highlight] Third Element in Array Linux is: Ubuntu |
The 3rd element of Array Linux is Ubuntu |
Output:
[Highlight] Length of Third Element is: 6 |
And the number of characters in the third element is six, as expected.
|
Let us summarize. Come back to our slides. | |
Display slide
Summary |
In this tutorial we learnt,
|
Display Slide 9
Assignment |
As an assignment.
Declare an array names of length 7 and find:
|
Watch the video available at the link shown below
It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it | |
Display Slide
|
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact@spoken-tutorial.org |
Display Slide
Acknowledgement |
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
Display Slide | The script has been contributed by FOSSEE and spoken-tutorial team.
Thank you for joining.
|