Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Justinzobel/e40f0cbfee1b2454f28b54f61d0d3506 to your computer and use it in GitHub Desktop.
Save Justinzobel/e40f0cbfee1b2454f28b54f61d0d3506 to your computer and use it in GitHub Desktop.
How to switch audio output from a terminal on Linux
Assumes you're using PulseAudio (fairly safe bet).
Find audio devices:
$ pactl list sinks | grep "Name: "
Name: alsa_output.pci-0000_01_00.1.hdmi-stereo
Name: alsa_output.usb-Corsair_Components__Inc._Corsair_Vengeance_1500-00.analog-stereo
Name: alsa_output.pci-0000_00_1f.3.analog-stereo
Copy the name of the sink you want into the variable devicename below and save as a bash script.
This can now be used with a hot-key to switch audio on the fly without opening anything.
It can alternatively be used as a base to switch audio to and from a device when a program launches.
For example, change output to speakers, open music app, when app closes, switch back to headphones.
#!/bin/bash
#Device name variable
devicename="alsa_output.usb-Corsair_Components__Inc._Corsair_Vengeance_1500-00.analog-stereo"
#change the default sink
pacmd "set-default-sink "$devicename""
#move all inputs to the new sink
for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
do
pacmd "move-sink-input $app "$devicename""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment