Skip to content

Instantly share code, notes, and snippets.

@jared-hughes
Created April 13, 2022 02:29
Show Gist options
  • Save jared-hughes/5d303b911f038057f0d997ecb90c53dc to your computer and use it in GitHub Desktop.
Save jared-hughes/5d303b911f038057f0d997ecb90c53dc to your computer and use it in GitHub Desktop.
Switch audio device
#!/bin/bash
# Switch audio device
# Based on https://askubuntu.com/a/1203350/647230
declare -i next_sink_index=`pacmd list-sinks | sed -n -e 's/[[:space:]][[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`
# 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');
do
pacmd "move-sink-input $app $next_sink_index"
done
# Notify change
notify-send -i audio-headphones "Sound output switched to $(pacmd list-sinks | awk '/index: '"$next_sink_index"'/{f=1} /device.description/{if(f==1){print;f=0};}' | sed -n -e 's/\s*device.description = "\(.*\)".*/\1/p')"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment