Unix & Linux
posix file-descriptors open-files
Updated Thu, 11 Aug 2022 20:36:46 GMT

Which programs use a file descriptor higher than 2?

Standard file descriptors <= 2 are opened by default. A program can write to or read from, a file descriptor after 2, without using the open system call to obtain such a descriptor. The program can then advertise in its manual, which file descriptors it is using and how. To make use of this, a POSIX shell can open a file and assign that file to a descriptor with the exec built-in. After that, the shell would start the program which will use that descriptor and file.

One reason for doing that would be if the program wants to have more than one output or input file, and does not want to specify them as command line arguments. If there was just one file, you could just redirect a standard file descriptor.

I have never seen a generally available program which would advertise such a thing in its manual. Does this happen in practice? Has anybody heard of such a thing?

Yes, I do want to stay within POSIX world - so no bash-only built-ins. I just want to know if there is such a program, not a shell built-in.


When you use process substitution with <(...) or >(...), bash will open a pipe to the other program on an arbitrary high file descriptor (I think it used to count up from 10, but now it counts down from 63) and pass the name as /dev/fd/N on the command line of the first program. This isn't POSIX, but other shells also support it (it's a ksh88 feature).

That's not exactly a feature of the program you're running though, it just sees /dev/fd/N and tries to open it like a regular file.

  • The Autoconf manual mentions some historic notes:

    A few ancient systems reserved some file descriptors. By convention, file descriptor 3 was opened to /dev/tty when you logged into Eighth Edition (1985) through Tenth Edition Unix (1989). File descriptor 4 had a special use on the Stardent/Kubota Titan (circa 1990), though we don't now remember what it was. Both these systems are obsolete, so it's now safe to treat file descriptors 3 and 4 like any other file descriptors.

  • Also while I did a google search for this I found a program called runit that uses file descriptors 4 and 5 for some purpose related to log rotation.

  • And quoting from the svlogd man page:

    If svlogd is told to process recent log files, (...). svlogd also saves any output that the processor writes to file descriptor 5, and makes that output available on file descriptor 4 when running processor on the next log file rotation.

Comments (1)

  • +1 – thank you for mention of runit svlogd that answers my question. Everything else that everybody wrote, unfortunately, does not understand the question. Not sure why, I really tried to make it really clear. — Aug 11, 2016 at 07:52