bash - return a value from shell script, into another shell script -
I know how to return an exit code, but I returned the result of the operation done in a shell script function I would like to do this, so I can finally use it in another script or function.
Something like
var1 = $ (myfunction) function2 var1 where my work is A + B = C
I saw in "return", but it will return a code, not a value.
I see in different sites, it tells me how to write the function, but I do not think how you actually return the value.
In C ++ you will use "variable name", but shell script will not allow this. It says that the variable is not in existence (which is logical, it is a variable created in a function, so when the function is released, then that memory space is given). Global variables can not be used because the function can be in a script and the calling function whose return value is required can vary.
myfunction a + b = c
Something like this might be: Just reverse the result:
$ myfunction () {$ echo ($ 1 + $ 2)); } Adds the number to the above myfunction and the result echoed. The return value can only be captured as you had it:
$ var = $ (myfunction 12 5) $ echo $ var 17 Build captures the standard from var = $ (myfunction) myfunction and saves it to var . Thus, when you want to return something from myfunction , just send it to the standard, as we did with the echo in the example above. In cases where you want to format the return value carefully, you should consider using printf instead of the echo. More: How to return multiple values
Let's define a function that generates two outputs:
$ f () {echo " Output1 "; "Output 2" echo; } $ F Output 1 Output 2 If you want to get those values back individually, then the most reliable way is to use arrays of bash:
$ a = ($ (F)) $ (f) via the above executed f , and Results a . We can try using a by using declare -p : $ declare -pa declare -aa = '([0] = "Output 1" [1] = "Output 2") ''
Comments
Post a Comment