function_name () {
list of commands
[ return value ]
}如果你愿意,也可以在函数名前加上关键字 function:function function_name () {
list of commands
[ return value ]
}函数返回值,可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值。#!/bin/bash
# Define your function here
Hello () {
echo "Url is http://see.xidian.edu.cn/cpp/shell/"
}
# Invoke your function
Hello运行结果:$./test.sh Hello World $调用函数只需要给出函数名,不需要加括号。
#!/bin/bash
funWithReturn(){
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 command
ret=$?
echo "The sum of two numbers is $ret !"运行结果:The function is to get the sum of two numbers... Input first number: 25 Input another number: 50 The two numbers are 25 and 50 ! The sum of two numbers is 75 !函数返回值在调用该函数后通过 $? 来获得。
#!/bin/bash
# Calling one function from another
number_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/像删除变量一样,删除函数也可以使用 unset 命令,不过要加上 .f 选项,如下所示:
$unset .f function_name如果你希望直接从终端调用函数,可以将函数定义在主目录下的 .profile 文件,这样每次登录后,在命令提示符后面输入函数名字就可以立即调用。
原文地址:http://blog.csdn.net/wu20093346/article/details/47253569