Two windows, same user, with bash prompts. In window-1 type:
$ mkfifo f; exec <f
So bash is now attempting to read from file descriptor 0, which is mapped to named pipe f
. In window-2 type:
$ echo ls > f
Now window-1 prints an ls and then the shell dies. Why?
Next experiment: open window-1 again with exec <f
. In window-2 type:
$ exec 3>f
$ echo ls >&3
After the first line above, window-1 wakes up and prints a prompt. Why? After the second line above, window-1 prints the ls
output and the shell stays alive. Why? In fact, now in window-2, echo ls > f
does not close the window-1 shell.
The answer must have to do with the existence of the file descriptor 3 from window-2 referencing the named pipe?!
exec <f
,bash
is not attempting to read fromf
, it is first attempting to open it. Theopen()
won't return until there is some process doing another open in write mode to the pipe (at which point the pipe will be instantiated, and the shell will read input from it).exec 3>f
is run, the first shell then gives a prompt. (Minor point, did you mean "in write mode" in your comment?)