Sending binary data to a TCP or UDP connection in bash

Sometimes you may want to send binary data to a TCP or UDP connection. You can send text data to a TCP port with telnet command but it doesn’t work for binary streams. There are some other alternatives which you can use like netcat or socat but it is also possible to send any data from TCP or UDP with just using bash shell’s internal functionality. For example to send text data to’s port 80 you can use:

$ echo "GET / HTTP/1.0" > /dev/tcp/

or to send 160 bytes of a binary data to port 9000:

$ dd if=binary.dat bs=160 count=1 > /dev/tcp/

it is also possible to use udp too:

$ dd if=binary.dat bs=160 count=1 > /dev/udp/

Please note that, there is no /dev/tcp or /dev/udp folder exists in the system, this redirection mechanism is a feature of the bash shell itself.