Does POSIX mandate that
stdin is 0,
stdout is 1 and
stderr is 2 or is this only a convention? Do other systems diverge from that convention or is it a safe assumption?
It seems that they are standardized in the POSIX spec,
The header shall define the following symbolic constants for file streams:
STDERR_FILENOFile number of stderr; 2.
STDIN_FILENOFile number of stdin; 0.
STDOUT_FILENOFile number of stdout; 1.
But also the POSIX docs on "
stdout- standard I/O streams" state,
This volume of POSIX.1-2017 defers to the ISO C standard.
The ISO ISO/IEC 9899:201x Standard state only,
The three predefined streams stdin, stdout, and stderr are unoriented at program startup.
It seems ISO C is relatively mute on this allowing the kernel to assign whatever it wants to the descriptors known as
STDIN. But that the POSIX docs on
unistd.h are explicit about what they should resolve to at that level.
Other Operating Systems
The overall format used for redirection is:
[n] redir-op word
The number n is an optional decimal number designating the file descriptor number; …
… The values 0, 1, and 2 have special meaning and conventional uses and are implied by certain redirection operations; they are referred to as standard input, standard output, and standard error, respectively. …
The general format for redirecting input is:
where the optional n represents the file descriptor number. If the number is omitted, the redirection shall refer to standard input (file descriptor 0).
… and a similar paragraph saying that
> is equivalent to
referring to standard output.