How to send a single UDP packet with Netcat

I needed this command to test UDP traffic to a particular endpoint at work today and thought I’d leave it written down here!

This netcat command sends a single UDP packet with the string “foo” to a particular IPv4 endpoint on a particular UDP port:

echo -n “foo” | netcat -u -w1 <host_ip> <port>