I found this line in a script. While I globally understand what it does–opening a bidirectional TCP connection–, I need some explanations on the syntax. Here’s the line:
exec 5<>"/dev/tcp/${SERVER}/${PORT}"
And my questions:
<and>are usually used to redirect IOs. What does it mean there? Is it usable in another context? How?- Why does it work, while
/dev/tcpdoesn’t exists? - Why 5? Can it be another number? What are the values allowed?
- Why is
execnecessary? (given nothing is actually executed)
Thanks.
It’s the same – input and output is redirected to fd 5.
It’s a special file: If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open a TCP connection to the corresponding socket.
Yes, it can be any value, but you need to ensure you don’t use an fd already in use.
exec means the redirection happens in the current shell, not within a subshell.