在重定向中使用时,<> 的 POSIX 定义行为是什么?

What is the POSIX defined behavior of <> when used in a redirect?

文件可以以读写模式打开并与之交互:

exec 4<> myfile

reader < myfile
writer >> myfile

下面的代码也有效,而且更简单:

reader <> myfile
writer >> myfile

这让我相信 <> 以读写模式打开文件,但在用作重定向时表现得像 <。我能找到的关于 <> 行为的唯一信息与其在 exec 中的使用有关。我无法在任何地方找到任何文档,说明它用作重定向时的行为是否实际定义,或者是否依赖于实现。

POSIX 规范中是否定义了此行为?便携吗?

来自2017 IEEE standards for "Shell Command Language"

2.7.7 Open File Descriptors for Reading and Writing

The redirection operator:

     [n]<>word

shall cause the file whose name is the expansion of word to be opened for both reading and writing on the file descriptor denoted by n, or standard input if n is not specified. If the file does not exist, it shall be created.

因此,任何合规的 shell 都应尊重输入输出重定向。