Skip to content

Instantly share code, notes, and snippets.

@jdrews
Created September 14, 2025 15:36
Show Gist options
  • Select an option

  • Save jdrews/5b1a22cf5d697673bcc3ea7e6598c8cb to your computer and use it in GitHub Desktop.

Select an option

Save jdrews/5b1a22cf5d697673bcc3ea7e6598c8cb to your computer and use it in GitHub Desktop.
#!/bin/bash
# get list of sinks/cards (for settings CARD1/CARD2)
# For each of your audio interfaces, run:
# pactl get-default-sink
CARD1="alsa_output.pci-0000_0a_00.1.hdmi-stereo-extra1" # Speakers
CARD2="alsa_output.usb-Focusrite_Scarlett_2i4_USB-00.HiFi__Line1__sink" # Headphones
CURRENT_SINK=$(pactl get-default-sink)
function setCard() {
if [ "$CURRENT_SINK" == "$1" ]
then
echo "Already using this Sink"
exit 1
fi
SINK=$(pactl set-default-sink "$1")
echo "Setting default sink to: $1";
notify-send --urgency=low "Audio Switching" "SINK: $1"
}
function toggleSinks() {
if [ "$CURRENT_SINK" == "$CARD1" ]
then
setCard $CARD2
else
setCard $CARD1
fi
}
function showHelp() {
echo "------------------------------------"
echo "AUDIO SINK SWITCHER"
echo " "
echo "$0 [options]"
echo " "
echo "options:"
echo "-h --help What you are looking at.."
echo "-p, --headphones Sets Headphones as output device"
echo "-s, --speakers Sets Speakers as output device"
echo "-t, --toggle Toggles the different output devices"
echo " "
echo "------------------------------------"
}
# check args length
if [ $# -eq 0 ]
then
echo "Toggling output devices (Speakers/Headset)"
toggleSinks
fi
# arg options
while test $# -gt 0; do
case "$1" in
-h|--help)
showHelp
exit 1
;;
-s|--headphones)
setCard $CARD2
exit 1
;;
-s|--speakers)
setCard $CARD1
exit 1
;;
-t|--toggle)
toggleSinks
echo "Toggling output devices (Speakers/Headset)"
exit 1
;;
*)
showHelp
exit 1
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment