Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script for Linux to set the next available audio output as the default system output, switching all current streams over to it.
# Switch to the next available audio output device and show a notification with
# all available devices and the one currently selected.
# You may need to edit /etc/pulse/ and change the appropriate line to:
# load-module module-stream-restore restore_device=false
# Heavily modified from:
new_sink=$(pacmd list-sinks | grep index | tee /dev/stdout | grep -m1 -A1 "* index" | tail -1 | cut -c12-)
new_sink_name=$(pactl list sinks | grep -A10 "Sink #$new_sink" | grep Description | cut -c15-)
echo "Setting default sink to: $new_sink: $new_sink_name";
pacmd set-default-sink $new_sink
pacmd list-sink-inputs | grep index | while read line; do
echo "Moving input $(echo $line | cut -f2 -d' ') to sink $new_sink"
pacmd move-sink-input `echo $line | cut -f2 -d' '` $new_sink
all_sinks=$(pactl list short sinks)
while read line; do
sink_num=$(echo $line | cut -d ' ' -f 1)
sink_name=$(pactl list sinks | grep -A10 "Sink #$sink_num" | grep Description | cut -c15-)
if [[ "$(echo $line | cut -d ' ' -f 1)" == "$new_sink" ]]; then
notif_msg="$notif_msg$sep<b>*$(echo $sink_name | tr a-z A-Z)</b>"
notif_msg="$notif_msg$sep $sink_name"
if [[ "$sep" == "" ]]; then
done <<< "$all_sinks"
notify-send --icon=audio-speakers --expire-time=3000 "Audio Output" "$notif_msg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.