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,

It seems ISO C is relatively mute on this allowing the kernel to assign whatever it wants to the descriptors known as STDOUT, STDERR, and STDIN. But that the POSIX docs on unistd.h are explicit about what they should resolve to at that level.

Other Operating Systems

| improve this answer | |
  • Almost anything hosted that's ANSI C compliant will usually follow the same convention. This includes pretty much everything that is an actual operating system (instead of something that's just some bare-bones libraries, like an Arduino for example). – Austin Hemmelgarn Apr 13 '18 at 19:30
  • 2
    @AustinHemmelgarn: That's not quite true, because the functions that take/return file descriptors (open(), close(), read(), write() etc.) are part of POSIX but not part of ANSI C - the C equivalents are fopen(), fwrite() etc. That said, the vast majority of ANSI C systems that people are likely to encounter commonly are either Unix-like or Windows-like, so in practice what you say is generally true :) – psmears Apr 13 '18 at 21:43

The POSIX Specification for Shell Command Language, Section 2.7, Redirection, says

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:
 [n ]<word

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 1>, referring to standard output.

| improve this answer | |

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.