Files can be opened in read-write mode and interacted with like so:
exec 4<> myfile
reader < myfile
writer >> myfile
The following code also works, and is simpler:
reader <> myfile
writer >> myfile
This makes me believe that <>
opens the file in read-write mode, but behaves as <
when used as a redirect. The only information I can find on the behavior of <>
is related to its use with exec
. I cannot find any documentation anywhere on whether or not its behavior when used as a redirect is actually defined, or if is implementation dependent.
Is this behavior defined in the POSIX specification anywhere? Is it portable?
From the 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.
Thus, any compliant shell should respect in-out redirection.