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?

1

1 Answer 1

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
2
  • 1
    How does close work on these fds? exec ${var1}>&- doesn't seem to work
    – Anirban
    Commented Sep 29, 2022 at 4:29
  • @Anirban: exec {var1}>&- (works in ksh, bash and zsh).
    – tom
    Commented May 20, 2023 at 14:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .