6

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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
maxschlepzig
  • 56,316
  • 50
  • 205
  • 279

1 Answers1

6

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
cuonglm
  • 150,973
  • 38
  • 327
  • 406