Easy Tutorial
❮ Linux Comm Dump Linux Comm Uudecode ❯

Shell Functions

Linux shell allows users to define functions, which can then be called at will within shell scripts.

The format for defining a function in shell is as follows:

[ function ] funname [()]

{
    action;
    [return int;]
}

Notes:

The following example defines a function and calls it:

Example

#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org

demoFun(){
    echo "This is my first shell function!"
}
echo "-----Function starts-----"
demoFun
echo "-----Function ends-----"

Output:

-----Function starts-----
This is my first shell function!
-----Function ends-----

Below is a function with a return statement:

Example

#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org

funWithReturn(){
    echo "This function will add two input numbers..."
    echo "Enter the first number: "
    read aNum
    echo "Enter the second number: "
    read anotherNum
    echo "The two numbers are $aNum and $anotherNum!"
    return $(($aNum+$anotherNum))
}
funWithReturn
echo "The sum of the two input numbers is $?!"

Output:

This function will add two input numbers...
Enter the first number: 
1
Enter the second number: 
2
The two numbers are 1 and 2!
The sum of the two input numbers is 3!

The return value of the function is obtained after calling it through $?.

Note: All functions must be defined before use. This means that the function must be placed at the beginning of the script, and it can only be used after the shell interpreter first encounters it. Calling a function simply uses its function name.


Function Parameters

In Shell, parameters can be passed to a function when it is called. Inside the function, the values of the parameters are obtained in the form of $n, for example, $1 represents the first parameter, $2 represents the second parameter...

Example of a function with parameters:

Example

#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org

funWithParam(){
    echo "The first parameter is $1!"
    echo "The second parameter is $2!"
    echo "The tenth parameter is $10!"
    echo "The tenth parameter is ${10}!"
    echo "The eleventh parameter is ${11}!"
    echo "The total number of parameters is $#!"
    echo "Output all parameters as a single string $*!"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

Output:

The first parameter is 1!
The second parameter is 2!
The tenth parameter is 10!
The tenth parameter is 34!
The eleventh parameter is 73!
The total number of parameters is 11!
Output all parameters as a single string 1 2 3 4 5 6 7 8 9 34 73!

Note that $10 cannot get the tenth parameter; to get the tenth parameter, ${10} must be used. When n >= 10, ${n} must be used to get the parameter.

Additionally, there are several special characters used for parameter handling:

Parameter Handling Description
$# The number of parameters passed to the script or function
$* Displays all parameters passed to the script as a single string
$$ The current process ID number of the script
$! The ID number of the last process running in the background
$@ Same as *, but each parameter is returned quoted.
$- Displays the current options used by the shell, same as the set command.
$? Displays the exit status of the last command. 0 indicates no error, any other value indicates an error.
❮ Linux Comm Dump Linux Comm Uudecode ❯