CLI to switch audio outputs on Linux
I typically have headphones permanently connected to my desktop computer but still find the need to quickly switch the audio to the desktop speakers. I’d prefer to avoid plugging/unplugging my headphones so came up with this approach.
This uses the Pulse Audio controller command: pactl
First identify your available audio outputs:
1 | $ pactl list short | grep alsa_output | grep -v monitor |
Then create some scripts to set the chosen output / sink.
/usr/local/bin/switch_2_headphones.sh
1 | #!/bin/bash |
/usr/local/bin/switch_2_speakers.sh
1 | #!/bin/bash |
Then setup some keyboard shortcuts to run the scripts. I chose ALT-SHIFT-S and ALT-SHIFT-H for speakers and headphones, respectively.
*UPDATE NOV 2022: * After upgrading to Mint 21 there needs to be an additional change to /etc/pulse/default.pa
for the CLI sink changes to work.
Change line load-module module-stream-restore
to load-module module-stream-restore restore_device=false
Then restart pulseaudio via pulseaudio --kill
then pulseaudio --start
.