Opening more than 10 file descriptors with ksh

file-descriptorsio-redirectionksh

The shell syntax for opening a file for writing and using its file descriptor is:

exec 3>output.log

With bash and zsh you also can write:

exec 13>output.log

Thus, later in your script you can redirect output like this:

generate-partx >&3
generate-partx >&13

And to close them:

exec 3>&-
exec 13>&-

The original ksh (tested 88 and 93) only seems to support file descriptor numbers 0 to 9 with that syntax.

Sure, one could argue that 10 open file descriptors should be enough for everyone and/or that nobody is using ksh anymore.

But sometimes it is not and you are.

Thus, my question: How to open more than 10 file descriptors in a ksh script?

Best Answer

In ksh, you can only use single digit for explicit file descriptor. With ksh93r and above, you can open more than 10 file descriptor by using the form:

{var}>filename

(bash and zsh copied this feature later).

ksh will pick available file descriptor greater than or equal to 10, store file descriptor number in variable var:

$ exec {var1}>/tmp/test1
$ echo "$var1"
10
$ exec {var2}>/tmp/test2
$ echo "$var2"
11