我创建了一个命名文件描述符{out}
:
$ exec {out}>out
但是当我尝试写入指定的文件描述符时,系统会使用文件描述符的名称创建一个新文件:
$ echo >&{out} $ find . -name {out} ./{out}
bash manual说:
Each redirection that may be preceded by a file descriptor number may instead be preceded by a word of the form {varname}.
如果我用数字来代替它,效果很好:
$ exec 3>out $ echo test >&3 $ cat out test
如何对命名的文件描述符做同样的事情?
最佳答案
大括号内的字符串只是 shell 将为您设置的变量的名称,其值是 shell 分配的文件描述符。大括号不是名称的一部分。为了使用它,只需在适当的上下文中扩展变量。
$ exec {out}>out
$ echo foobar >&$out
$ cat out
foobar
在您的示例中,文件 {out}
由
echo >&{out}
不是初始 exec
,它的重定向创建了文件 out
,将分配的文件描述符存储在变量 out
中。
关于bash - 如何写入 Bash 中的命名文件描述符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26607577/