Skip to content

Instantly share code, notes, and snippets.

@teklynk
Created September 27, 2025 01:36
Show Gist options
  • Select an option

  • Save teklynk/1ab826c1686a055fb9b77a10781b33d9 to your computer and use it in GitHub Desktop.

Select an option

Save teklynk/1ab826c1686a055fb9b77a10781b33d9 to your computer and use it in GitHub Desktop.
A Simple Bash Script to Toggle Audio Output on Linux Mint
#!/bin/bash
# pactl list short sinks
#61 alsa_output.usb-SteelSeries_SteelSeries_Arctis_1_Wireless-00.analog-stereo PipeWire s16le 2ch 48000Hz RUNNING
#63 alsa_output.pci-0000_00_1f.3.analog-stereo PipeWire s32le 2ch 48000Hz SUSPENDED
# Set your actual sink names here
SPEAKERS="alsa_output.pci-0000_00_1f.3.analog-stereo"
HEADSET="alsa_output.usb-SteelSeries_SteelSeries_Arctis_1_Wireless-00.analog-stereo"
# Get the currently active default sink
CURRENT_SINK=$(pactl get-default-sink)
if [ "$CURRENT_SINK" = "$SPEAKERS" ]; then
pactl set-default-sink "$HEADSET"
# Move all current playing streams to the new sink
pactl list short sink-inputs | while read -r input; do
pactl move-sink-input $(echo "$input" | cut -f1) "$HEADSET"
done
echo "Switched to headset"
play /usr/share/mint-artwork/sounds/plug.oga
notify-send "Switched to headset"
else
pactl set-default-sink "$SPEAKERS"
pactl list short sink-inputs | while read -r input; do
pactl move-sink-input $(echo "$input" | cut -f1) "$SPEAKERS"
done
echo "Switched to speakers"
play /usr/share/mint-artwork/sounds/plug.oga
notify-send "Switched to speakers"
fi
@vladdoster
Copy link

vladdoster commented Sep 28, 2025

Suggestion:

To reduce redundant code, set a variable new_sink to string value of headset or speakers in the respective conditional branch and consolidate duplicated code that plays the sound and notification to end of file and use the new_sink variable.

echo "Switched to ${new_sink}"
play /usr/share/mint-artwork/sounds/plug.oga
notify-send "Switched to ${new_sink}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment