Search code examples
bashioexecfile-descriptor

How to use a variable to indicate a file descriptor in bash?


I want to use a bash variable to indicate a file descriptor, like this:

id=6
file=a
exec $id<>$file

But the usage is wrong:

-bash: exec: 6: not found

So, how to use a variable to indicate a file descriptor in exec command?


Solution

  • You have to use eval and put the entire expression in quotes.

    eval "exec $id<>$file"
    

    And do that every time you want to use $id.