Returns any value from the bash function.

Source: Internet
Author: User
Return any value from the bash function. unlike other programming languages, a Bash function is more like a subprogram, that is, its return value. it can only be a number and 0 indicates success, if the value is not 0, the operation fails. If you want to return values other than numbers in the bash function, there are three methods: www.2cto. c... return any value from the bash function. unlike other programming languages, a Bash function is more like a subprogram, that is, its return value. it can only be a number and 0 indicates success, if the value is not 0, the operation fails. If you want to return values other than numbers in the bash function, there are three methods: www.2cto.com 1. suppose we want to return a string, the simplest method is: Shell code function myfunc () {myresult = 'the value'} myfunc echo $ myresult through a global variable myresult, to save the return value. The disadvantage of using global variables is self-evident. Www.2cto.com 2. method 2: Shell code function myfunc () {local myresult = 'some value' echo "$ myresult"} result = $ (myfunc) # or result = 'myfunc' echo $ result calls a shell function as a sub-program and writes the return value to the standard output of the sub-program to return any string. 3. method 3: Shell code function myfunc () {local _ resultvar = $1 local myresult = 'some value' eval $ __resultvar = "'$ myresult'"} myfunc result echo $ result, similar to the function value returned by referencing in C ++. Www.2cto.com can also be combined with the last two methods: Shell code function myfunc () {local _ resultvar = $1 local myresult = 'some value' if ["$ __resultvar"]; then eval $ __resultvar = "'$ myresult'" else echo "$ myresult" fi} myfunc result echo $ result result2 = $ (myfunc) echo $ result2, maximum flexibility.
 

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.