Linux重定向; 可以在重定向之间切换?

Car*_*ndo 1 linux redirect

我希望能够在重定向之间切换.即:

重定向#1将是:不重定向到stdout,将输出重定向到文件.

重定向#2将是:不重定向到文件...而是重定向到stdout

是否可以在运行时执行此操作?

dma*_*a_k 6

在这里它适用于bash(对于C来说它将是相同的):

#!/bin/bash

echo begin experiment

exec 3>&1-

exec > to_file

echo this goes to file
echo another line to file

exec 1>&3-

echo this goes again to stdout
Run Code Online (Sandbox Code Playgroud)