#!/bin/sh
sum ()
{
echo $ (($1+$2))
return $ (($1-$2))
}
Sum $
c=$ (sum $)
echo $?
Echo $c
Execute command:./bashtest 1
The operating result is:
12--sum Results
10--echo $? result because the value of return is ten
12--echo $c result, the value is 12, so C will not be appended with the return value, echo $c call the Sum function, so print a
our understanding of the return value in the shell is biased, the return value in the shell is generally the return value after the execution of the command, typically the command executes successfully returned 0, the execution fails to return 1, after the command executes, you can use echo $? To see what the return value is, in this case, Because the function has a return statement, the function itself defines a return value that can be viewed through echo $? The value of c=$ (sum $) is not equal to the value of return, stating that the shell does not have the function as we defined in C, as if C=abs (I,J) Such a return value is called.
someone has said this:
Basically, it means that the function in the shell does not assign the return value of a function to a variable like any other language?
Such as
C=sum_test $
Echo $c Nickname: Hxl time: 2010-06-09-10:34:00reply hxl
no .... Nickname: Lkk2003rty time: 2010-06-09-11:27:00
################ from Http://blog.163.com/[email protected]/blog/static/44780245201062865644141/################# #
Correct method:Function Plus () {
Read-p "Input number1:" Number1
Read-p "Input number2:" Number2
#echo $ (($number 1+ $number 2)) #两组括号的意思是做算术运算
echo ' expr $number 1 + $number 2 '
}
res=$ (plus)
echo "plus result is $res"
using Echo, call and get with res=$ (plus)
About the return value of functions in the shell