With bash 4.1.2 and 4.3.48, the following script gives the expected result:
#!/bin/bash returnSimple() { local __resultvar=$1 printf -v "$__resultvar" '%s' "ERROR" echo "Hello World" } returnSimple theResult echo ${theResult} echo Done.
Conclusion of the expected result:
$ ./returnSimple Hello World ERROR Done.
However, when stdout is passed from a function to another process, assigning the __resultvar variable no longer works:
#!/bin/bash returnSimple() { local __resultvar=$1 printf -v "$__resultvar" '%s' "ERROR" echo "Hello World" } returnSimple theResult | cat echo ${theResult} echo Done.
Unexpected conclusion:
$ ./returnSimple Hello World Done.
Why printf -v does not work in the second case? Should printf -v not write the value to the result variable regardless of whether the output of the function is passed to another process?
linux unix bash
Andreas Fester
source share