I'm trying to catch a return value from a function in bash, that modify a global variable. Works perfectly with funtions with no parameters:
#!/bin/bash
tes=2
testfunction(){
tes=3
tes_str="string result"
return 0
}
if output=testfunction; then
echo "OK"
else
echo "KO"
fi
echo $tes
echo $tes_str
But no with parameters:
#!/bin/bash
tes=2
testfunction(){
tes=3
tes_str="string result"
return 0
}
if output=$(testfunction "AA" "BB"); then
echo "OK"
else
echo "KO"
fi
echo $tes
echo $tes_str
Because for bash, parameters ("AA" or "BB") are a command, and I must put it in backets (but if use backets, can't modify global variables). How can I do it? I'm stucked. Regards
Works perfectly with funtions with no parametersNo it doesn't, your code is invalid.output=testfunctiononly assigns the texttestfunctiontooutput.testfunction. Your second example does. You can easily verify this by doing aecho heloo >&2inside testfunction.