相关疑难解决方法(0)

在没有临时文件的情况下将 STDERR 和 STDOUT 重定向到不同的变量

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下将函数的重定向func到变量b

bash io-redirection shell-script stderr variable

9
推荐指数
1
解决办法
6354
查看次数

标签 统计

bash ×1

io-redirection ×1

shell-script ×1

stderr ×1

variable ×1