Bash函数是否有可能将值输出到文件描述符,并仅将其分配给变量?

问题描述 投票:0回答:1

我想模拟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>)
bash shell stdout stdin file-descriptor
1个回答
0
投票

这样的事情应该做:

f() {
  echo stdout_value
  echo stderr_value >&2
  echo return_value >&3
}

{ var=$(f 3>&1 >&4); } 4>&1
© www.soinside.com 2019 - 2024. All rights reserved.