Get the current volume level in OS X Terminal CLI?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












17















I would like to check the current volume level from the CLI on my Mac. I know I can set it like this:



osascript -e 'set volume <N>'


But that doesn't seem to work when trying to get the current volume level.



$ osascript -e 'get volume'
4:10: execution error: The variable volume is not defined. (-2753)









share|improve this question


























    17















    I would like to check the current volume level from the CLI on my Mac. I know I can set it like this:



    osascript -e 'set volume <N>'


    But that doesn't seem to work when trying to get the current volume level.



    $ osascript -e 'get volume'
    4:10: execution error: The variable volume is not defined. (-2753)









    share|improve this question
























      17












      17








      17


      5






      I would like to check the current volume level from the CLI on my Mac. I know I can set it like this:



      osascript -e 'set volume <N>'


      But that doesn't seem to work when trying to get the current volume level.



      $ osascript -e 'get volume'
      4:10: execution error: The variable volume is not defined. (-2753)









      share|improve this question














      I would like to check the current volume level from the CLI on my Mac. I know I can set it like this:



      osascript -e 'set volume <N>'


      But that doesn't seem to work when trying to get the current volume level.



      $ osascript -e 'get volume'
      4:10: execution error: The variable volume is not defined. (-2753)






      command-line osx audio volume






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '13 at 19:06









      Cory KleinCory Klein

      5,582216084




      5,582216084




















          3 Answers
          3






          active

          oldest

          votes


















          18














          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.






          share|improve this answer























          • get volume settings doesn't really differentiate between 0, 0.1, and 0.01. It doesn't show decimal values, making it quite useless.

            – A-B-B
            Apr 18 '14 at 22:36












          • @A-B-B, great suggestion. Thanks for contributing.

            – ghoti
            Oct 28 '15 at 0:22


















          5














          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 ;





          share|improve this answer
































            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"





            share|improve this answer






















              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f102552%2fget-the-current-volume-level-in-os-x-terminal-cli%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              18














              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.






              share|improve this answer























              • get volume settings doesn't really differentiate between 0, 0.1, and 0.01. It doesn't show decimal values, making it quite useless.

                – A-B-B
                Apr 18 '14 at 22:36












              • @A-B-B, great suggestion. Thanks for contributing.

                – ghoti
                Oct 28 '15 at 0:22















              18














              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.






              share|improve this answer























              • get volume settings doesn't really differentiate between 0, 0.1, and 0.01. It doesn't show decimal values, making it quite useless.

                – A-B-B
                Apr 18 '14 at 22:36












              • @A-B-B, great suggestion. Thanks for contributing.

                – ghoti
                Oct 28 '15 at 0:22













              18












              18








              18







              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.






              share|improve this answer













              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 25 '13 at 20:23









              tmarttmart

              19613




              19613












              • get volume settings doesn't really differentiate between 0, 0.1, and 0.01. It doesn't show decimal values, making it quite useless.

                – A-B-B
                Apr 18 '14 at 22:36












              • @A-B-B, great suggestion. Thanks for contributing.

                – ghoti
                Oct 28 '15 at 0:22

















              • get volume settings doesn't really differentiate between 0, 0.1, and 0.01. It doesn't show decimal values, making it quite useless.

                – A-B-B
                Apr 18 '14 at 22:36












              • @A-B-B, great suggestion. Thanks for contributing.

                – ghoti
                Oct 28 '15 at 0:22
















              get volume settings doesn't really differentiate between 0, 0.1, and 0.01. It doesn't show decimal values, making it quite useless.

              – A-B-B
              Apr 18 '14 at 22:36






              get volume settings doesn't really differentiate between 0, 0.1, and 0.01. It doesn't show decimal values, making it quite useless.

              – A-B-B
              Apr 18 '14 at 22:36














              @A-B-B, great suggestion. Thanks for contributing.

              – ghoti
              Oct 28 '15 at 0:22





              @A-B-B, great suggestion. Thanks for contributing.

              – ghoti
              Oct 28 '15 at 0:22













              5














              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 ;





              share|improve this answer





























                5














                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 ;





                share|improve this answer



























                  5












                  5








                  5







                  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 ;





                  share|improve this answer















                  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 ;






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 2 '16 at 12:03

























                  answered Sep 2 '16 at 11:55









                  docgyneco69docgyneco69

                  6113




                  6113





















                      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"





                      share|improve this answer



























                        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"





                        share|improve this answer

























                          0












                          0








                          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"





                          share|improve this answer













                          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"






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 28 at 16:23









                          iolsmitiolsmit

                          1112




                          1112



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f102552%2fget-the-current-volume-level-in-os-x-terminal-cli%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown






                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?