1

I am having difficulty understanding redirects how can we use exec <> as a Linux command what does it mean. Can you give an example and explain?

  • exec 7<>/path/to/file means in "bash" what int fd = open("/path/to/file", O_RDWR); dup2(fd, 7); close(fd); means in C, with error checking included. Since you cannot seek on file descriptors in the shell, its uses are rather limited. One practical use of it is to open fifos without blocking: mkfifo fifo; ... exec 7<>fifo will not block since you open both its read and its write end at the same time. – mosvy Nov 7 at 20:06
  • so are you saying like it creates a file descriptor where you can send stuff sort of "stdin" or "stdout" – remember5thofnovember Nov 7 at 20:31
  • stdin is just fd 0, stdout is fd 1, that's nothing special about them. If you omit the number before <>, it will default to 0 in bash, dash, zsh, etc and to 1 in ksh93. – mosvy Nov 7 at 20:56
  • I wonder if the question is "what does it do", or if it's "what's a sensible use for it". Or both. – ilkkachu Nov 9 at 12:35
1

The <> redirection operator opens the file given by the word on its right for both reading and writing. The file descriptor number on its left (or 0 in most shells if you omit the number) will be connected to the file.

Example:

$ echo Hello >file

This writes the string Hello to the file, followed by a newline.

$ { read -r message <&3; printf 'Got "%s"\n' "$message"; echo Good bye >&3; } 3<>file
Got "Hello"

This opens the file for both reading and writing on file descriptor 3. A string is read from file descriptor 3 and printed to the terminal. Then a new string is written to the same file descriptor.

$ cat file
Hello
Good bye

The file now contains the additional string that we wrote in the last command. The string was inserted after the newline after Hello since that was where the file pointer was when we wrote to the file. Had there been more data in the file after Hello, the Good bye string would have overwritten some of that.

-1

Try to understand with cat.

You can use cat like normal:

cat > somefile now you can write some input and after terminating with control+d your input will redirected with the > operator to somefile.

Now try:

cat <> somefile

cat is doing the same like before.

But there will be no input in somefile.

Now try:

cat <(echo "hello world")> somefile

Now there will be some input in somefile with an extensive use of redirect operators.

  • cat <> somefile won't write anything to somefile, because <> by default opens the file on stdin (fd 0). It just opens it in read-write mode, but cat only reads from it. There would a difference between cat < foo and cat <> foo if foo doesn't exist: <> would create it first and there's no error. That last example (cat <(...)> somefile) doesn't even use the <> operator, so I'm not sure how that's relevant. – ilkkachu Nov 9 at 12:38
  • @John, if I'm reading between the lines here, I think ilkkachu is trying to add some description about the <> operator, and then commented that the last example in your answer doesn't directly apply to the <> operator. If you feel like the comment doesn't help improve your answer, then feel free to flag it as "no longer needed". At this point, I believe ilkkachu is trying to be helpful, not antagonistic. – Jeff Schaller Nov 9 at 19:05
  • @JeffSchaller I don't know. If I read his comment under the this question... – John Goofy 21 hours ago
  • @John your comment trailed off, so I'm not sure what you meant. Their comment on the question seems (to me) intended to clarify the question. Only the OP can say for sure whether it's a useful comment or not, since it's their question. – Jeff Schaller 20 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.