BASH functions
hello world example
Section titled “hello world example”A simple function named hello that will print out “hello world!”
#!/bin/bash
function hello { echo hello world!}
helloThis will output
hello world!hello greeting example
Section titled “hello greeting example”This function takes 2 arguments - a first name and last name.
#!/bin/bash
function greet { echo greetings $1 and $2 family.}
greet Joe BloggsThis will output
greetings Joe and Bloggs familyReturning an exit code from a function
Section titled “Returning an exit code from a function”#!/bin/bash
function add_2_numbers { total=`expr $1 + $2` return $total;}
add_2_numbers 2 5echo answer is $?The above will output
answer is 7A function can only return values between 0 and 255.
##Links Linux Documentation Project - Advanced Bash-Scripting - Functions