How can I execute a bash script when the external IP is changed. I wonder if there is a hook in macOS to run such a script.
Your computer has no way to detect by itself that the external IP has changed, as this is handled by the router that translates your private IP to the public one.
So you must poll either the router itself, or an external site.
The router can be interrogated through SNMP le if it supports this protocol. If it doens't but has a command line (CLI) access then you can use it with a language like expect. If the router is only accesible through a web interface, this is doable but more complex.
You can actually avoid polling if the router support sending SNMP traps or has an event handler, then it is the router that can notify you computer about the change, and then you run your script on reception of such notification.