5.9.1 Using close()’s Return Value

close() is a function. The return value is −1 if the argument names something that was never opened with a redirection, or if there is a system problem closing the file or process. In these cases, gawk sets the predefined variable ERRNO to a string describing the problem.

In gawk, when closing a pipe or coprocess (input or output), the return value is the exit status of the command, as described in Table 5.1. Otherwise, it is the return value from the system’s close() or fclose() C functions when closing input or output files, respectively. This value is zero if the close succeeds, or −1 if it fails. BWK awk also returns the same values from close().

SituationReturn value from close()
Normal exit of commandCommand’s exit status
Death by signal of command256 + number of murderous signal
Death by signal of command with core dump512 + number of murderous signal
Some kind of error−1

Table 5.1: Return values from close() of a pipe

The POSIX standard is vague; it says that close() returns zero on success and a nonzero value otherwise. In general, different implementations vary in what they report when closing pipes; thus, the return value cannot be used portably. (d.c.)