Function
Creating functions
#!/bin/bash

function hello(){
	echo "Hello World!"
}

hello
			
Pass parameters
#!/bin/bash

function hello(){
	echo "Name: $1, Age: $2"
}

hello Lin 39
			
Return
#!/bin/bash

function getName(){
	return 10
}

getName

echo $?
			
#!/bin/bash

function getName(){
	name="Yanhua"
}

name="Lin"
getName

echo $name
			
#!/bin/bash

function getName(){
	echo "Yanhua"
}

name=$(getName)

echo $name
			
Call function in function
#!/bin/bash

function f1()
{
	echo "Call f2 from f1 ..."
	f2
}

function f2()
{
	echo "f2 ..."
}

f1
			
Function Call from Prompt
# f6.sh
#!/bin/bash

function f1()
{
	echo "Call f2 from f1 ..."
	f2
}

function f2()
{
	echo "f2 ..."
}
			
. f6.sh # source f6.sh
f1 # call function f1 from prompt

unset -f f1 # unset a function
			
Call function define in another script
# f6.sh
#!/bin/bash

function f1()
{
	echo "Call f2 from f1 ..."
	f2
}

function f2()
{
	echo "f2 ..."
}
			
# f7.sh
#!/bin/bash

source f6.sh # import functions defined in f6.sh
f1 # call function f1
			
Recursion
#!/bin/bash

function factorial () { 
    if (($1 == 1))
    then
        echo 1
        return
    else
        echo $(( $( factorial $(($1 - 1)) ) * $1 ))
    fi
}

factorial 10
			
Reference