An interesting Bourne shell limitation

January 11, 2007

Presented in illustrated form, on at least Solaris 8, FreeBSD, and OpenBSD:

$ exec 9>/dev/null
$ exec 10>/dev/null
exec: 10: not found

(And the shell exits.)

The genuine Bourne shell only allows redirection to (or from) single-digit file descriptors; if you give multiple digits, it instead gets parsed as 'exec 10 >/dev/null'.

(The limitation has been faithfully copied by at least some Bourne shell reimplementations and was retained in ksh, but bash has dispensed with it, so you won't see this behavior on (most) Linux machines.)

This limitation is implicitly in the V7 sh manpage, which says about redirection:

If one of the above is preceded by a digit then the file descriptor created is that specified by the digit (instead of the default 0 or 1).

In the grand tradition of reading Unix manpages, using the singular 'digit' means that multiple ones aren't allowed.

This wording was carried on into the Solaris sh manpage, but not the FreeBSD one. The FreeBSD situation is interesting, since I believe the FreeBSD shell was rewritten from scratch; clearly the people doing the rewrite knew about (and decided to preserve) the limitation, but then they didn't document it.

(One might argue that the FreeBSD usage of '[n]> file' in the manpage implies that 'n' can only be a single digit, but I think that the ice is thin, if only because they use things like '[n1]<&n2'.)

Written on 11 January 2007.
« A gotcha with inetd/xinetd and popular UDP services
Xen versus VMware for me »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Thu Jan 11 01:46:08 2007
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.