You can do
5<&"$fd"
: duplicate fd $fd
onto fd 5 (where 5 is literal) in any Bourne-like shell (though beware that when yash is in posix mode, that won't work if $fd
is not open for reading).
{fd}<&-
: close fd $fd
in zsh, bash, ksh93
But you can't do "$fd">anything
as you don't want echo "$something">file
to not output the content of $something
into file
if that content just happens to contain only decimal digits.
In zsh, bash and ksh93, you can use {fd}> something
for a fd above 9 to be allocated automatically and stored into $fd
, but that's not what you're asking.
As far as I know, there's no other way than to use eval
.
You can limit eval
usage to the redirection only by using exec
:
(eval "exec $TWO>&1"; exec date) >> logfile
It's the same situation in the fish
shell.
It's also true in rc
and derivatives, where the syntax is >[3=5]
, and both fds have to be literal.