0% found this document useful (0 votes)
1 views2 pages

Introduction To Functions

Functions in Bash are reusable blocks of code that can be defined and called multiple times. They can accept command line arguments and return output or exit status. Proper syntax and definition order are essential for effective use of functions.

Uploaded by

mengheng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

Introduction To Functions

Functions in Bash are reusable blocks of code that can be defined and called multiple times. They can accept command line arguments and return output or exit status. Proper syntax and definition order are essential for effective use of functions.

Uploaded by

mengheng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like