Created
September 27, 2025 01:36
-
-
Save teklynk/1ab826c1686a055fb9b77a10781b33d9 to your computer and use it in GitHub Desktop.
A Simple Bash Script to Toggle Audio Output on Linux Mint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Suggestion:
To reduce redundant code, set a variable
new_sinkto string value ofheadsetorspeakersin the respective conditional branch and consolidate duplicated code that plays the sound and notification to end of file and use thenew_sinkvariable.