Skip to content

Instantly share code, notes, and snippets.

Embed
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.
#!/bin/bash
#
# rotate-audio-output.sh
#
# 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/default.pa and change the appropriate line to:
# load-module module-stream-restore restore_device=false
#
# Heavily modified from: https://askubuntu.com/a/740002
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
done
notif_msg=''
sep=''
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>"
else
notif_msg="$notif_msg$sep $sink_name"
fi
if [[ "$sep" == "" ]]; then
sep='\n'
fi
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.