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.
    – user313992
    Nov 7, 2019 at 20:06
  • so are you saying like it creates a file descriptor where you can send stuff sort of "stdin" or "stdout" Nov 7, 2019 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.
    – user313992
    Nov 7, 2019 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, 2019 at 12:35

2 Answers 2


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.


$ 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
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.


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, 2019 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, 2019 at 19:05
  • @JeffSchaller I don't know. If I read his comment under the this question...
    – John Goofy
    Nov 14, 2019 at 16:01
  • @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
    Nov 14, 2019 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .