Search this site





bourne (sh, ksh, bash, zsh, ...) file descriptor fun

Yeah, so having just figured this out now (I'm sure it's in a manpage somewhere) -
You can open file descriptors at will by doing:

exec 3> file_output

The 3 here is the number of the file descriptor, and can be any number really. What this lets you do now is write to that file descriptor:

echo "Hello there" >&3

Now, if you look at the contents of file_output you'll see that it contains "Hello there" - neat?

To close a file descriptor do this:

exec 3>&-

again, where 3 is the number of the file descriptor you want to close.