BASH/C3/More-on-functions/English
Title of script: More on functions
Author: Lavitha Pereira
Keywords: Video tutorial, Bash shell, function, function call
|
|
Display Slide 1 | Dear friends, Welcome to the spoken tutorial on More on functions |
Display Slide 2 | In this tutorial, we will learn
|
Display Slide 3Prerequisites
|
To follow this tutorial you should have knowledge of Shell Scripting in BASH.
If not, for relevant tutorials please visit our website which is as shown,(http://www.spoken-tutorial.org) |
Display Slide 4
System requirements |
For this tutorial I am using
Please note, GNU Bash version 4 or above is recommended to practice this tutorial. |
Let us first learn how to pass an argument to a function, and its usage. | |
Let me open a file 'function_parameters.sh' | |
Highlight #!/usr/bin/env bash | This is the shebang line. |
Highlight say_welcome()
{ |
Our function name is say_welcome
|
Highlight echo "Welcome to $1 $2" | $1 is the first positional parameter
$2 is the second postional parameter |
Highlight } | Close curly bracket closes the function definition. |
Highlight
say_welcome "Bash" "learning" |
Here, the function 'say_welcome' is called with arguments.
|
Highlight
say_welcome "functions in" "Bash"
|
In a similar manner, I will call the same function with a different set of arguments.
say_welcome space within double quote functions in space and within double quote Bash. |
Save and Switch to terminal | Save the file and go to the terminal. |
Type
chmod +x function_parameters.sh>>Press Enter
|
Type:chmod space plus x space function underscore parameters dot sh
|
Type
./function_parameters.sh>>Press Enter |
Type dot slash function underscore parameters dot sh
Press Enter. |
Terminal
[Output] Welcome to Bash learning Welcome to functions in Bash
|
We see that the positional parameters were substituted by the arguments passed to the function.
|
Display slide 5
Local and Global variables in functions |
* In Bash, variables can be declared as local variables and global variables.
|
Display slide 6
Local and Global variables in functions |
Global variable:
|
Let us learn these 2 ways to declare a variable within a function. | |
Let me open a file 'function_local.sh' | |
Highlight #!/usr/bin/env bash | This is the shebang line. |
say_hello(){ | Function name is say underscore hello |
Highlight local first_name=$1
#Local variable |
Here, variable first_name is declared with keyword local.
|
Highlight last_name=$2
# Global variable |
A variable declared without any keyword, is treated as a global variable.
|
Highlight echo "Hello $first_name $middle_name $last_name"
Highlight } |
In this echo line, we will display the value of variables
After this, we close this function. |
Highlight middle_name="K"
#global variable |
Now, here the variable middle_name is declared without keyword.
|
Highlight say_hello "Pratik" "Patil" | Once again, we will call the function here.
|
Highlight
echo "My first name is: $first_name" echo "My middle name is: $middle_name" echo "My last name is: $last_name" |
These echo statements will display the value of variables
|
Please keep in mind that variable first_name is a local variable. | |
Save and Switch to terminal | Save the file and go to the terminal. |
Type
chmod +x function_local.sh>>Press Enter |
Type:chmod space plus x space function underscore local dot sh
|
Type ./function_local.sh>> Press Enter | Now type
dot slash function underscore local dot sh
|
Terminal
[Output] Hello Pratik K Patil |
The first line of output displays the message Hello Pratik K Patil.
Here, the variable first_name that contains value “Pratik” is local.
|
Now, let us see how the local variable behaves outside the function. | |
Terminal
[Output] My first name is: My middle name is: k My last name is: Patil |
Here, nothing is displayed in first_name.
|
Hope the difference is clear to you.
| |
Display Slide 7
Summary |
In this tutorial, we learnt
|
Display Slide 7
Assignment |
As an assignment.
Write a program,
|
Display Slide 8
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial About the Spoken Tutorial Project |
Watch the video available at the link shown below.
|
Display Slide 9
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
For more details, please write to contact@spoken-tutorial.org |
Display Slide 10
Acknowledgement |
Spoken Tutorial Project is a part of the Talk to a Teacher project.
|
Display Slide 11 | The script has been contributed by FOSSEE and Spoken-Tutorial teams.
Thank you for joining. |