Question

I need to set the system volume in a bash script, but I have no idea how to do this. Should I use alsactl? Are there some values in /proc or /sys that I could use?

Was it helpful?

Solution

You should use the amixer tool.

Run amixer without parameters to get a list of mixer controls.
Use commands like this:

amixer set Master 50%     # set absolute
amixer set Master 2dB+    # set relative
amixer set Master unmute

OTHER TIPS

You can also use pactl.

pactl set-sink-volume 0 60%  # set absolute
pactl set-sink-volume 0 +10% # set relative
pactl set-sink-mute 0 0      # unmute

In particular, the pactl set-sink-mute 0 0 unmute command works on my computer where amixer set Master unmute doesn't.

I've got a nice vol script in my ~/bin to help me do the trick ...

#!/bin/sh

export XDIALOG_NO_GMSGS=1

case ${1} in
    +|-) VOL=( $(amixer set Master 10dB${1} |tail -1|tr -d "[]") )
        Xdialog --no-buttons --title "Vol" --infobox ${VOL[3]} 50x30+32+32 ;;
    *[0-9]) VOL=( $(amixer set Master "${1}%" |tail -1|tr -d "[]") )
        Xdialog --no-buttons --title "Vol" --infobox ${VOL[3]} 50x30+32+32 ;;
    *) printf "Usage: vol [+]|[-]|[0-100]\n" >&2 ;;
esac

If you don't have Xdialog installed a simple echo ${VOL[3]} will give you some alsamixer feedback.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top