Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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 use two sound devices, normal analogue output and sound over HDMI. When I'm not using the TV I leave it turned off and switch to the speakers. As I do this regularly I would like a script to do it for me.

I did have a script which switched between the two, but only for the current application producing sound. I have lost it and can't seem to find the internet source.

Can anyone help me recreate this script?

share|improve this question
This seems to change the active profile for a specific card, doesn't help me switch between two cards. – D4nC00per Jan 8 '13 at 19:59
I think what I want to do is move a sink to a different card? – D4nC00per Jan 8 '13 at 20:01
up vote 2 down vote accepted

I found the script finally. This script will move all input sinks (i.e. your music, video sound etc.) to your other sound device. If you run the script again it will move all sinks back to your original sound device. (I have only tested this on two sound devices).

Instruction on how to setup:

  1. Paste the following code into a file using gedit.
  2. Save the file as (or some other file name) in your home directory.
  3. Change the file permissions to allow execution.

    • Right click the file and choose properties.
    • Select the permissions tab.
    • Tick the "Allow executing file as program" tick box


    • chmod +x ./
  4. Double click the file and click run. It will change the output sound device.

I then added an option on the settings icon in the unity launcher for ease of access using Ubuntu Tweak. Enjoy!



declare -i sinks=(`pacmd list-sinks | sed -n -e 's/\**[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`)
declare -i sinks_count=${#sinks[*]}
declare -i active_sink_index=`pacmd list-sinks | sed -n -e 's/\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`
declare -i next_sink_index=${sinks[0]}

#find the next sink (not always the next index number)
declare -i ord=0
while [ $ord -lt $sinks_count ];
echo ${sinks[$ord]}
if [ ${sinks[$ord]} -gt $active_sink_index ] ; then
let ord++

#change the default sink
pacmd "set-default-sink ${next_sink_index}"

#move all inputs to the new sink
for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
pacmd "move-sink-input $app $next_sink_index"

#display notification
declare -i ndx=0
pacmd list-sinks | sed -n -e 's/device.description[[:space:]]=[[:space:]]"\(.*\)"/\1/p' | while read line;
if [ $(( $ord % $sinks_count )) -eq $ndx ] ; then
    notify-send -i notification-audio-volume-high --hint=string:x-canonical-private-synchronous: "Sound output switched to" "$line"
let ndx++

All credit goes to the guys here, I just re-found the answer to my question.

share|improve this answer
Great script, thanks. Please note that in the notify-send line there is whitespace that shouldn't be there after 'private-' . It should be --hint=string:x-canonical-private-synchronous: "Sound output switched to" "$line" – Nin Jul 29 '15 at 6:54
Fixed, thanks for pointing that out. – D4nC00per Jul 29 '15 at 11:25

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.