0% found this document useful (0 votes)
3 views

Bash Functions

The document explains Bash functions, which are sets of commands that enhance script readability and reduce code repetition. It covers defining functions, variable scope (global and local), return values, and passing arguments to functions. Additionally, it includes a task to write a Bash script for calculating the summation of two numbers.

Uploaded by

Omar Fahmy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Bash Functions

The document explains Bash functions, which are sets of commands that enhance script readability and reduce code repetition. It covers defining functions, variable scope (global and local), return values, and passing arguments to functions. Additionally, it includes a task to write a Bash script for calculating the summation of two numbers.

Uploaded by

Omar Fahmy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Bash Functions

Subtitle
Function:

 A Bash function is essentially a set of commands that can be


called numerous times.
 The purpose of a function is to help you make your bash scripts
more readable and to avoid writing the same code repeatedly
Defining Bash Functions

 The syntax for declaring a bash function is straightforward.


 Functions may be declared in two different formats:
1.The first format starts with the function name, followed by
parentheses. This is the preferred and more used format.
The second format starts with the reserved
word function,
followed by the function name.
Some points to be noted:

The commands between the curly braces ({}) are called the body of the
function.

 The curly braces must be separated from the body by spaces or newlines.

Defining a function doesn’t execute it. To invoke a bash function, simply use
the function name. Commands between the curly braces are executed
whenever the function is called in the shell script.

The function definition must be placed before any calls to the function.
When using single line “compacted” functions, a semicolon ; must follow the
last command in the function.
Variables Scope

 Global variables are variables that can be accessed from


anywhere in the script regardless of the scope.
 In Bash, all variables by default are defined as global
Local variables can be declared within the function body with the local keyword
and can be used only inside that function.
Return Values

 Unlike functions in “real” programming languages, Bash functions don’t


allow you to return a value when called.

 When a bash function completes, its return value is the status of the last
statement executed in the function, 0 for success and non-zero decimal
number in the 1 - 255 range for failure.

 The return status can be specified by using the return keyword, and it is
assigned to the variable $?.
Passing Arguments to Bash Functions

 To pass any number of arguments to the bash function simply put them right
after the function’s name, separated by a space.

•The passed parameters are $1, $2, $3 … $n, corresponding to the position of the
parameter after the function’s name.
•The $0 variable is reserved for the function’s name.
•The $# variable holds the number of positional parameters/arguments passed to the
function.
•The $* and $@ variables hold all positional parameters/arguments passed to the
function.
When double-quoted, "$*" expands to a single string separated by space (the first
character of IFS) - "$1 $2 $n".
•When double-quoted, "$@" expands to separate strings - "$1" "$2" "$n".
•When not double-quoted, $* and $@ are the same.
Task :write a bash script to make function that calculate
summation of two numbers.

You might also like