There's a command line utility,
tvservice built into Raspbian that will tell you the state of the HDMI.
To detect the full state, you can use the command:
tvservice -s. On my RPi this currently outputs:
state 0x40001 [NTSC 4:3], 720x480 @ 60.00Hz, interlaced
The states (that I've gathered) are as follows:
0x40001 Not initialized and HDMI cable is disconnected
0x40002 Not initialized but HDMI cable is connected
0x120002 Standby mode
0x120005 HDMI disconnected
If you're less interested in the additional details
tvservice -n may be slightly simpler to parse. It gives the current name of connected device. Example:
[E] No device present
Additionally, if connectivity is touch and go, you may find the
-M flag useful. It monitors for events concerned with the HDMI port.
As Ghanima pointed out in the comments, tvservice is also available in Arch under