Bash Function

Define a function

#!/bin/bash 

testfunc () { 
    echo "$# parameters"; 
    for i in $@; 
        do echo $i; 
    done;
}
testfunc a 'b c d'


function e {
	echo $1 
}  
e Hello
e World

Send parameter

IBM: bash parameters

[ian@pinguino ~]$ testfunc () { echo "$# parameters"; echo "$@"; }
[ian@pinguino ~]$ testfunc
0 parameters

[ian@pinguino ~]$ testfunc a b c
3 parameters
a b c
[ian@pinguino ~]$ testfunc a "b c"
2 parameters
a b c

$ testfunc () { echo "$# parameters"; for i in $@; do echo $i; done;}
$ testfunc a 'b c d'
2 parameters
a
b
c
d

Return value

function myfunc()
{
    local  myresult='some value'
    echo "$myresult"
}

result=$(myfunc)   # or result=`myfunc`
echo $result

Ways to let function returns a value

Homepage
Comments

Hide Comments