From The Linux Programming Interface
In an interactive shell, these three file
descriptors 0, 1 and 2 normally refer to the terminal under which the shell is running.
-
Does “the terminal under which the shell is running” mean the
controlling terminal of the session to which the interactive shell
belongs?If yes, what if the shell’s session doesn’t have a controlling terminal?
-
When the shell is created from its parent process, will the shell automatically create connection betwee file descriptors 0, 1 and 2 and the terminal, in each of
the following cases (inheritance from the parent process of the shell):-
if “the terminal under which the shell is running” or the controlling terminal has already been opened at a file descriptor
which is not 0, 1 and 2? -
if the file descriptors 0, 1 and 2 have already been connected to a file which is not “the terminal under which the shell is running”
or the controlling terminal?
-
if “the terminal under which the shell is running” or the controlling terminal has already been opened at a file descriptor
- What if the shell in the quote is noninteractive?
Thanks.
Btw, just assume “shell” is POSIX or bash.
Related How can we disconnect a file descriptor from any file?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
No.
This is done by the various implementations of getty, by open-controlling-tty, by the forked pty-slave-side part of an SSH server, by the forked pty-slave-side part of a GUI terminal emulator, or some such.
And notice that the text quoted has no implication that the shell does this. You have created a whole load of questions based upon a false premise pulled out of thin air.
And you knew that getty does this, because you wrote that it did in “When is the process session led by login started?“, written at the same time as this question.
Further reading
- What is the difference between getty and agetty?
- “askfirst” getty with systemd (“press enter to activate this console”)
- Jonathan de Boyne Pollard. “Virtual terminal login“. nosh Guide. Softwares.
- Jonathan de Boyne Pollard. “Real terminal login“. nosh Guide. Softwares.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0