How to get volume level from the command line?

A one-liner to parse amixer's output for volume in a status bar:

awk -F"[][]" '/dB/ { print $2 }' <(amixer sget Master)

Edit: As of November 2020, the updated amixer for Arch Linux is 1.2.4 which has no 'dB' in the output. So, the command should replaced by:

awk -F"[][]" '/Left:/ { print $2 }' <(amixer sget Master)

You can use amixer to do this.

Examples

$ amixer get Master
Simple mixer control 'Master',0
  Capabilities: pvolume pswitch pswitch-joined penum
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 65536
  Mono:
  Front Left: Playback 65536 [100%] [off]
  Front Right: Playback 65536 [100%] [off]

You can also change it and mute it like so:

set volume 75%

$ amixer set Master 75%
Simple mixer control 'Master',0
  Capabilities: pvolume pswitch pswitch-joined penum
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 65536
  Mono:
  Front Left: Playback 49152 [75%] [on]
  Front Right: Playback 49152 [75%] [on]

mute/unmute

$ amixer set Master toggle
Simple mixer control 'Master',0
  Capabilities: pvolume pswitch pswitch-joined penum
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 65536
  Mono:
  Front Left: Playback 65536 [100%] [on]
  Front Right: Playback 65536 [100%] [on]

You can quiet the output if you don't want to see any of it with the --quiet switch.

$ amixer --quiet set Master 75%
$ 

Right

amixer sget Master | grep 'Right:' | awk -F'[][]' '{ print $2 }'
85%

Left

amixer sget Master | grep 'Left:' | awk -F'[][]' '{ print $2 }'
85%

Sound server

If you are not using pulseaudio as default you can specify to amixer what server to use with -D pulse

amixer -D pulse sget Master | grep 'Left:' | awk -F'[][]' '{ print $2 }'
85%