#函數返回值:
#1. return(0-255) 通過$?獲得
#2. echo 通過`function`獲得,非結果echo到文件或者/dev/null ,返回值是數組,字符串,大於255的整數,可以使用echo
#3. 聲明全局變量 declare 返回值是數組,字符串,大於255的整數,可以使用echo
# echo 最好用,返回數組也要依靠echo
declare g_res
function test1 {
local a=1
local b=2
local c=$[$a+$b]
return $c
}
function test2 {
local a=4
if [ $a -eq 4 ]
then
echo "four"
else
echo " null"
fi
}
function test3 {
g_res=1
}
function test4 {
local i
local arr
for (( i=0; i<5; ++i))
do
arr[$i]=$i
done
echo ${arr[*]}
}
test1
echo "function test1 return : "$?
test2Result=`test2`
echo "function test2 return :"$test2Result
test3
echo "function test3 return : "$g_res
test4Res=`test4`
echo "function test4 return : "$test4Res
運行結果:
function test1 return : 3
function test2 return :four
function test3 return : 1
function test4 return : 0 1 2 3 4