#!/bin/bashfunWithReturn(){ echo "The function is to get the sum of two numbers..." echo -n "Input first number: " read aNum echo -n "Input another number: " read anotherNum echo "The two numbers are $aNum and $anotherNum !" return $(($aNum+$anotherNum))}funWithReturn# Capture value returnd by last commandret=$?echo "The sum of two numbers is $ret !"
运行结果:
The function is to get the sum of two numbers...Input first number: 25Input another number: 50The two numbers are 25 and 50 !The sum of two numbers is 75 !
函数返回值在调用该函数后通过 $? 来获得。
再来看一个函数嵌套的例子:#!/bin/bash# Calling one function from anothernumber_one () { echo "Url_1 is http://see.xidian.edu.cn/cpp/shell/" number_two}number_two () { echo "Url_2 is http://see.xidian.edu.cn/cpp/u/xitong/"}number_one
运行结果:
Url_1 is http://see.xidian.edu.cn/cpp/shell/Url_2 is http://see.xidian.edu.cn/cpp/u/xitong/