Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using Ubuntu Lucid (10.04). In my shell script running as root I want to detect whether an audio cable (analog jack) is connected to the laptop or not. How do I do that? I don't need a portable solution, I need something, no matter how hacky, that works on my laptop.

share|improve this question
What makes you think this is possible? – michas May 19 '13 at 16:14
Cables cannot be detected. Depending on the hardware, a device at the other end of the cable can be detected; in practice, this is only used for headphones detection. Show which HDA codec you have and how it is wired up to the jack. – CL. May 19 '13 at 16:14
@CL: The device at the other end is an externally powered speaker, which is turned on. I don't think it has any smart self-announcement capabilities. How do I figure out which HDA codec I have? – pts May 19 '13 at 17:17
The hardware can detect it, because it mutes the internal speaker as soon as I connect the external speaker. – pts May 19 '13 at 17:18
But usually that switch-over is handled by an analog switch which is not instrumented for programatic access. As michas noted, it probably isn't possible. – msw May 19 '13 at 18:30

One way could perhaps be to use amixer.

Jack plugged in:

$ amixer -c 0 contents
numid=29,iface=CARD,name='Front Headphone Jack'
  ; type=BOOLEAN,access=r-------,values=1
  : values=on

Jack not plugged in:

$ amixer -c 0 contents
numid=29,iface=CARD,name='Front Headphone Jack'
  ; type=BOOLEAN,access=r-------,values=1
  : values=off

So for that specific one I could do:

amixer -c 0 contents | \
awk -F"," '
$1 == "numid=29" {
} c && /: values/ {
   split($0, a, "=")
   print a[2];

giving output of on or off.

One can also use commands and specify by iface + name etc. e.g.:

Get list by:

$ amixer -c 0 controls

Where -c 0 specifies card, not needed if default or only one. Then e.g.:

$ amixer -c 0 cget numid=29,iface=CARD

$ amixer -c 0 cget numid=29,iface=CARD | awk -F"=" 'NR == 3 {print $2;}'

I came across a piece of software once, believe it used Tcl/Tk, that displayed pin-power for all ports on the computer + lots of other HW information. It was a nice piece of software – but I can't find it again. I have looked trough 12 old HDD's with no luck. I used it to debug some jack-ports. So yes, it is definitively possible to poll status of a specific port.

Jack is a pain to search the web for due to JACK. It makes it close to impossible.

I have some C-code that do some Soundcard information polling. Have to look if I can find it.

share|improve this answer
amixer output is subject to change, parsing /proc/asound/card0/codec#0 directly may work better. Alternatively, see unix.stackexchange.com/questions/25776/… – kevinf Jun 14 at 17:56

Since you said "no matter how hacky" . . .

Get an external sound card that you can connect to your laptop via USB. Plug the analog jack into that. Plug in your external sound card when you want to connect the audio jack and have your bash script detect whether or not the external sound card is connected via USB.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.