Yes, in bash you can use file descriptor to read and / or write and you can use any number. However , since 0 , 1, and 2 are used for STDIN , STDOUT, and STDERR , we avoid these. In addition, numbers greater than 9 are used internally, so we also avoid them.
Here's how we can open a file descriptor to do the following:
To read:
fd<source
Record:
fd>source
Read / write:
fd<>source
where fd is the number that describes the file descriptor , and the source can be either file , or even another file descriptor (although you must add & ).
If you use exec for file descriptors, then this allows the changes to take effect in the current shell. Otherwise, they are local to the function or loop.
Example 1 -
The following use of fd will be local to the while loop, and you cannot use it outside:
while read line <&3; do echo $line; done 3<test.file
Example 2 -
The next use of fd will be visible in the current shell
exec 3<test.file while read line <&3; do echo $line; done
source share