How to send a single UDP packet with Netcat

At work today I needed this command to test UDP traffic to a particular endpoint, 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 address on a particular UDP port:

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