Where there is no wheel to reinvent?
networksetup -getairportnetwork en1 | cut -c 25-
- this is what you would use on 10.6, 10.7 changed the name "Hardware Port" from "Airport" to "Wi-Fi", and therefore you would cut one letter less,
aru$ networksetup -getairportnetwork en1 | cut -c 24- Yorimichi
If the device has a name other than en1 , first you need to get the correct device name, and not the corresponding SSID:
networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print $2}' | xargs networksetup -getairportnetwork
Sacrilicious Dec 17 2018-11-11T00: 00Z
source share