I found an answer from here:
if system_profiler SPAudioDataType | grep --quiet Headphones; then echo plugged inelse echo not plugged infi
I found an answer from here:
if system_profiler SPAudioDataType | grep --quiet Headphones; then echo plugged inelse echo not plugged infi