How to use a variable to indicate a file descriptor in bash?
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
.
The accepted answer is correct, but as of bash 4.1, you can use automatic file descriptor allocation, and in that case you don't need eval
:
file=a
exec {id}<>"$file"
Then you can use it like this:
echo test >&${id}
or:
fsck -v -f -C ${id} /dev/something