func() {
echo 'hello'
echo 'This is an error' >&2
}
a=$(func)
b=???
Run Code Online (Sandbox Code Playgroud)
我想将 stderr 重定向到b
变量而不创建临时文件。
echo $b
# output should be: "This is an error"
Run Code Online (Sandbox Code Playgroud)
有效但使用临时文件的解决方案:
touch temp.txt
exec 3< temp.txt
a=$(func 2> temp.txt);
cat <&3
rm temp.txt
Run Code Online (Sandbox Code Playgroud)
所以问题是,如何在不需要临时文件的情况stderr
下将bash函数的重定向func
到变量b
?