Introduction to Functions
Function is a block of code
We can re-use this block of code any number of time in scripts
Synatx:
function Name_of_the_function { command1 ; command2 ; ….. Command_N; }
Or
function Name_of_the_function {
Command1
Command2
.
.
Command_N
}
Or
Name_of_the_function() {command1 ; command2 ; ….. Command_N; }
Or
Name_of_the_function() {
Command1
Command2
.
Command_N
}
Learn How to Automate Repetitive Tasks with Bash Shell Scripting
Introduction to Functions…
Points to remember:
Function is a block of code with custom name
We can call/re-use this block with given custom name with any number of times
Function is a like a script / command
If it is a script then we can pass command line argument to a function
Syntax: functionName [arguments]
Arguments can be accessed using ${1} ${2} ….
If it is a command then we can store the output of a command into a variable
Syntax: FunctionOutput=$(functionName [arguments])
If it is a command then we can also find the exit status of a function using $?, return is the key send the exit status from a
function
Function should be defined before its calling
Learn How to Automate Repetitive Tasks with Bash Shell Scripting