bash - 如何写入 Bash 中的命名文件描述符?

标签 bash file-descriptor

我创建了一个命名文件描述符{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/

相关文章:

c - 在命名管道 (FIFO) 上选择会导致无限循环

linux - 无法删除/var/log/acpid 文件

mysql - 从 bash 输出 sql 输出

bash - shell变量BASH_SOURCE的由来是什么?

c - 没有 read() 的文件描述符为空或 "flush"?

bash - 从 Bash 函数返回文件描述符

c - 与之前 fork 的子进程共享一个套接字

Bash 将存储的 "boolean"值与什么进行比较?

bash - 计算 git 存储库中的行数

python 2.7 弹出 : what does `close_fds` do?