2

For example, if I assign

TWO=2

...the following fails (with date: invalid date '2'):

date >> logfile $TWO>&1

Of course, I can always do

eval "date >> logfile $TWO>&1"

Is this the only way to run expressions that include stream redirections where at least some of the file descriptors appear as variables?

1 Answer 1

3

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.

You must log in to answer this question.

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