Get the current volume level in OS X Terminal CLI?

You should find that get volume settings will return an object containing among other things the output volume and the alert volume. So for example you could do this to retrieve the entire object:

osascript -e 'get volume settings'

or rather maybe this to grab just the output volume (e.g. rather than the alert volume):

osascript -e 'set ovol to output volume of (get volume settings)'

... but note that not all audio devices will have direct software control over volume settings. For example your display audio should have control; however, a firewire or USB i/o board probably would not have those settings under software control (since they might be physical knobs). If the particular setting is not under the control of software then it will show up in the object returned from get volume settings as "missing value" or something like that.


I committed a very humble bash script named "chut". As I was fed up with the sys volume requiring a float point as input (0 to 10 step 0.1) but outputing an integer with a step 14 ranging from 0 to 100.

Go figure... If anyone interested: http://github.com/docgyneco69/chut

In its full glory:

#!/bin/bash
## CHUT script
## Note: regex [[:digit:]] requires a relatively recent shell
## easy to change with a sed cmd if needed
## applescript arg is not fully bullet proofed for sneaky cmds
## but as no outside arg is passed by the script I kept the usual
## arg format for code readibility (and pure laziness)

# init _x and curr_vol with defaults values (muting)
_x='- 100' ; curr_vol='0' ;

function _usage {echo -e "CHUT is a simple cmd exe to change the system audio volume.
USAGE chut [][-][--][+][++]
      no arg will mute (default)
      [-][+] [--][++] to decrease or increase the volume
      [+++] to set to the maximum
      [-h][--help] display this message
NOTE sys sets volume as float (0-10/0.1) but outputs int (0-100/14)" ; exit 1 ; } ;

# set _x by looping $1 then break as we only use 1st arg, -h or --help to print usage
while [[ "$1" ]]; do case "$1" in
    "-h"|"--help")  _usage      ;;
    "-")        _x='- 0.5'  ;;
    "--")       _x='- 1.0'  ;;
    "+")        _x='+ 0.5'  ;;
    "++")       _x='+ 1.0'  ;;
    "+++")      _x='+ 100'  ;;
    *)      _x='- 100'  ;; # unrecognized values will mute
esac ; break ; done ;

# get current volume value from system (sys volume is 0 to 100 step 14)
curr_vol=$(/usr/bin/osascript -e "get volume settings" | cut -d ',' -f1 | tr -dc [[:digit:]]) ;

# set new volume via _x - use bc for floating point, escape potential errors, 
# print value with one decimal - test & echo the new volume value via applescript
curr_vol=$( printf "%.1f" "$( echo "$curr_vol / 14 $_x" | bc -l 2>&-)" ) ;
(/usr/bin/osascript -e "set Volume "\"$curr_vol"\" ") && \
echo $(/usr/bin/osascript -e "get volume settings" | cut -d ',' -f1 | tr -dc [[:digit:]]) ;

exit 0 ;

Getting and setting the volume using the same scale 1..100:

# Get current volume as a number from 0 to 100
current_vol=$(osascript -e 'output volume of (get volume settings)')

# Prank co-worker by playing loud noise/music
osascript -e "set volume output volume 100"
afplay sabotage.m4a

# (Re-)set to saved volume as a number from 0 to 100
osascript -e "set volume output volume $current_vol"