Send UDP messages with /dev/udp

Dev Tip 6/99

The bash shell comes with two pseudo-devices for TCP and UDP network communication and/dev/tcp and /dev/udp. To use either in bash, you need to read or write to the device appending the host and port to the end of the path — e.g., /dev/tcp/ The primary reason I use the pseudo-devices is it’s easier for me to remember than the netcat. But if I wanted a portable solution, then netcat is the winner. Either case, learning of bash's TCP and UDP pseudo-devices tickled my brain.

For my day to day work, the /dev/udp comes in handy for sending statsd metrics to the local statsd server process on PlanGrid servers. statsd has a wire protocol that looks like:

# Example: counting 200 status codes for nginx:

Here is how to send the above metric via bash's UDP pseudo-device:

echo "nginx.status_200:1|c" >/dev/udp/

The other portable way is with netcat (nc):

echo "nginx.status_200:1|c" | nc -u -w0 8125