Well, reading the topics that DK Bose and vanadium suggested and having done some more research to learning about bash script and Linux PulseAudio sound server, I came up with one solution:
We need to save the following script in a file (preferably in /usr/local/bin/
, as explained here), mark it as executable (by run chmod +x
over it) and then assigned it to a keyboard shortcut (optionally as explained here).
#!/bin/bash
sound_output=$(pacmd list-cards | grep "active")
if [[ $sound_output = *"analog-stereo"* ]]; then
sound_output=hdmi-stereo
else
sound_output=analog-stereo
fi
pactl set-card-profile 0 output:"$sound_output"
echo -e "PulseAudio sound server output set to $sound_output"
Explaning:
#!/bin/bash
ensures that the interpreter for this script is bash, enabling extended syntax such as [[ ]].
- As cited here, the syntax
$( )
is "command substitution". This runs a command and use it's output to compose the line action. This way we save the
return of pacmd list-cards | grep "active"
in sound_output
variable.
- As explained here, the use of
*
in the condition turn it into a substring check.
- As answered here,
pactl set-card-profile 0 output: [card-profile]
is the command to switch the sound server output, as
wished.