gawk: Special FD

1 
1 5.7 Special Files for Standard Preopened Data Streams
1 =====================================================
1 
1 Running programs conventionally have three input and output streams
1 already available to them for reading and writing.  These are known as
1 the "standard input", "standard output", and "standard error output".
1 These open streams (and any other open files or pipes) are often
1 referred to by the technical term "file descriptors".
1 
1    These streams are, by default, connected to your keyboard and screen,
1 but they are often redirected with the shell, via the '<', '<<', '>',
1 '>>', '>&', and '|' operators.  Standard error is typically used for
1 writing error messages; the reason there are two separate streams,
1 standard output and standard error, is so that they can be redirected
1 separately.
1 
1    In traditional implementations of 'awk', the only way to write an
1 error message to standard error in an 'awk' program is as follows:
1 
1      print "Serious error detected!" | "cat 1>&2"
1 
1 This works by opening a pipeline to a shell command that can access the
1 standard error stream that it inherits from the 'awk' process.  This is
1 far from elegant, and it also requires a separate process.  So people
1 writing 'awk' programs often don't do this.  Instead, they send the
1 error messages to the screen, like this:
1 
1      print "Serious error detected!" > "/dev/tty"
1 
1 ('/dev/tty' is a special file supplied by the operating system that is
1 connected to your keyboard and screen.  It represents the "terminal,"(1)
1 which on modern systems is a keyboard and screen, not a serial console.)
1 This generally has the same effect, but not always: although the
1 standard error stream is usually the screen, it can be redirected; when
1 that happens, writing to the screen is not correct.  In fact, if 'awk'
1 is run from a background job, it may not have a terminal at all.  Then
1 opening '/dev/tty' fails.
1 
1    'gawk', BWK 'awk', and 'mawk' provide special file names for
1 accessing the three standard streams.  If the file name matches one of
1 these special names when 'gawk' (or one of the others) redirects input
1 or output, then it directly uses the descriptor that the file name
1 stands for.  These special file names work for all operating systems
1 that 'gawk' has been ported to, not just those that are POSIX-compliant:
1 
1 '/dev/stdin'
1      The standard input (file descriptor 0).
1 
1 '/dev/stdout'
1      The standard output (file descriptor 1).
1 
1 '/dev/stderr'
1      The standard error output (file descriptor 2).
1 
1    With these facilities, the proper way to write an error message then
1 becomes:
1 
1      print "Serious error detected!" > "/dev/stderr"
1 
1    Note the use of quotes around the file name.  Like with any other
1 redirection, the value must be a string.  It is a common error to omit
1 the quotes, which leads to confusing results.
1 
1    'gawk' does not treat these file names as special when in
1 POSIX-compatibility mode.  However, because BWK 'awk' supports them,
1 'gawk' does support them even when invoked with the '--traditional'
1 option (⇒Options).
1 
1    ---------- Footnotes ----------
1 
1    (1) The "tty" in '/dev/tty' stands for "Teletype," a serial terminal.
1