About the return value of functions in the shell

Source: Internet
Author: User

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.