我想模拟Bash函数的返回值,我想知道是否可以使用临时文件描述符来传递该值。
换句话说:
function myfunction {
# print `stdout_value` to stdout
# print `stderr_value` to stderr
# print `return_value` to FD3 (or other)
}
# the values printed to stderr/stdout should be printed, but only
# `return_value` should be assigned to `myvalue`
myvalue=$(myfunction <FDs manipulation>)
这样的事情应该做:
f() {
echo stdout_value
echo stderr_value >&2
echo return_value >&3
}
{ var=$(f 3>&1 >&4); } 4>&1