By Daniel Lundin:
zmq_push () { m=$(cat) && echo \
-e $(printf '\\x01\\x00\\x%02x\\x00%s' \
$((1 + ${#m})) "$m") | nc -q1 $@; }
Note: this actually works. It acts as a PUSH or DEALER socket, sending one message to a PULL or ROUTER or DEALER socket. It implements the 0MQ wire level protocol.
Upgrade from Jim Cheetham:
bash versions above 2.04 compiled with —enable-net-redirections enable redirections to a bash-specific filename of /dev/tcp/_host_/_port_ (i.e. /dev/tcp does not exist on your filesystem), and therefore we can rewrite the above code to remove the fork into netcat.
We can also simplify the code to use $(</dev/stdin) to avoid the cat and use printf more directly to avoid invoking an external echo, by doubling the backslashes. We also make this ZMTP/1.0 compliant by fixing the length field, which was off-by-one above.
zmq_push () {
m=$(</dev/stdin) && \
printf "$(printf '\\\\x01\\\\x00\\\\x%02x\\\\x00%s' ${#m} "$m")" \
>/dev/tcp/$1/$2
}
printf "Hello World!" | zmq_push localhost portnumber
This makes the code more bash-specific than before. Other shells might be better off with the former method.