How to get only the version number of php?

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











up vote
9
down vote

favorite












I want to get only the version of php installed on CentOS.



Output of php -v



PHP 7.1.16 (cli) (built: Mar 28 2018 13:19:29) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies


I tried this following:



php -v | grep PHP | awk 'print $2'


But the output I got was:



7.1.16
(c)


How can I get only 7.1.16?










share|improve this question



















  • 1




    ... | head -1 or there might be better ways
    – Vlastimil
    9 hours ago










  • Oh, head -1 is very simple and elegent. Thanks man.
    – The One
    9 hours ago














up vote
9
down vote

favorite












I want to get only the version of php installed on CentOS.



Output of php -v



PHP 7.1.16 (cli) (built: Mar 28 2018 13:19:29) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies


I tried this following:



php -v | grep PHP | awk 'print $2'


But the output I got was:



7.1.16
(c)


How can I get only 7.1.16?










share|improve this question



















  • 1




    ... | head -1 or there might be better ways
    – Vlastimil
    9 hours ago










  • Oh, head -1 is very simple and elegent. Thanks man.
    – The One
    9 hours ago












up vote
9
down vote

favorite









up vote
9
down vote

favorite











I want to get only the version of php installed on CentOS.



Output of php -v



PHP 7.1.16 (cli) (built: Mar 28 2018 13:19:29) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies


I tried this following:



php -v | grep PHP | awk 'print $2'


But the output I got was:



7.1.16
(c)


How can I get only 7.1.16?










share|improve this question















I want to get only the version of php installed on CentOS.



Output of php -v



PHP 7.1.16 (cli) (built: Mar 28 2018 13:19:29) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies


I tried this following:



php -v | grep PHP | awk 'print $2'


But the output I got was:



7.1.16
(c)


How can I get only 7.1.16?







php version






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 mins ago









sebasth

6,41321644




6,41321644










asked 9 hours ago









The One

1,08661528




1,08661528







  • 1




    ... | head -1 or there might be better ways
    – Vlastimil
    9 hours ago










  • Oh, head -1 is very simple and elegent. Thanks man.
    – The One
    9 hours ago












  • 1




    ... | head -1 or there might be better ways
    – Vlastimil
    9 hours ago










  • Oh, head -1 is very simple and elegent. Thanks man.
    – The One
    9 hours ago







1




1




... | head -1 or there might be better ways
– Vlastimil
9 hours ago




... | head -1 or there might be better ways
– Vlastimil
9 hours ago












Oh, head -1 is very simple and elegent. Thanks man.
– The One
9 hours ago




Oh, head -1 is very simple and elegent. Thanks man.
– The One
9 hours ago










7 Answers
7






active

oldest

votes

















up vote
8
down vote



accepted










On my system:



$> php -v | grep ^PHP | cut -d' ' -f2
7.0.32-0ubuntu0.16.04.1


as grep PHP matches every PHP string it encounters.



The ^PHP means "match only the string 'PHP' when it is at the start of a line".



Obviously, this works if the output format of php -v is consistent across versions/builds.



For reference, the whole output was:



PHP 7.0.32-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.32-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies





share|improve this answer



























    up vote
    22
    down vote













    Extending Jeff Schaller's answer, skip the pipeline altogether and just ask for the internal constant representation:



    $ php -r 'echo PHP_VERSION;'
    7.1.15


    You can extend this pattern to get more, or less, information:



    $ php -r 'echo PHP_MAJOR_VERSION;'
    7


    See the PHP list of pre-defined constants for all available.



    Benefit? More performant than pipelines and doesn't rely on a defined output format of php -v.






    share|improve this answer



























      up vote
      7
      down vote













      There are different ways, I like to use look behind:



      php -v | grep -Po '(?<=^PHP )[^ ]+'


      or



      php -v | grep -Po '(?<=PHP )([0-9.]+)'





      share|improve this answer




















      • Thanks Ravexina. The above command worked like a charm.
        – The One
        9 hours ago

















      up vote
      7
      down vote













      If you've installed php via the package manager (e.g. RPM or yum), then you can query the version from there:



      rpm -q --queryformat="%VERSION" php


      Alternatively, you can ask php to tell you its version directly:



      php -r 'echo phpversion();'





      share|improve this answer





























        up vote
        4
        down vote













        Since you started with awk, here's an awk solution:



        $ php -v | awk '/^PHP/print $2'
        7.2.10





        share|improve this answer



























          up vote
          1
          down vote













          If you want to do it with just a single function being piped, you can try using sed like this:



          php -v | sed -e '/^PHP/!d' -e 's/.* ([0-9]+.[0-9]+.[0-9]+).*$/1/'


          First it deletes any line that doesn't begin with PHP, then it clips the version from that line assuming it is the first sequence in the form of x.y.z.



          Or, if you want something closer to your original script, simply put ^ in the front of your grep pattern to only look for lines that start with PHP:



          php -v | grep ^PHP | awk 'print $2'





          share|improve this answer



























            up vote
            0
            down vote













            php -v | awk NR==1'print $2'





            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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f471521%2fhow-to-get-only-the-version-number-of-php%23new-answer', 'question_page');

              );

              Post as a guest






























              7 Answers
              7






              active

              oldest

              votes








              7 Answers
              7






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              8
              down vote



              accepted










              On my system:



              $> php -v | grep ^PHP | cut -d' ' -f2
              7.0.32-0ubuntu0.16.04.1


              as grep PHP matches every PHP string it encounters.



              The ^PHP means "match only the string 'PHP' when it is at the start of a line".



              Obviously, this works if the output format of php -v is consistent across versions/builds.



              For reference, the whole output was:



              PHP 7.0.32-0ubuntu0.16.04.1 (cli) ( NTS )
              Copyright (c) 1997-2017 The PHP Group
              Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
              with Zend OPcache v7.0.32-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies





              share|improve this answer
























                up vote
                8
                down vote



                accepted










                On my system:



                $> php -v | grep ^PHP | cut -d' ' -f2
                7.0.32-0ubuntu0.16.04.1


                as grep PHP matches every PHP string it encounters.



                The ^PHP means "match only the string 'PHP' when it is at the start of a line".



                Obviously, this works if the output format of php -v is consistent across versions/builds.



                For reference, the whole output was:



                PHP 7.0.32-0ubuntu0.16.04.1 (cli) ( NTS )
                Copyright (c) 1997-2017 The PHP Group
                Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
                with Zend OPcache v7.0.32-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies





                share|improve this answer






















                  up vote
                  8
                  down vote



                  accepted







                  up vote
                  8
                  down vote



                  accepted






                  On my system:



                  $> php -v | grep ^PHP | cut -d' ' -f2
                  7.0.32-0ubuntu0.16.04.1


                  as grep PHP matches every PHP string it encounters.



                  The ^PHP means "match only the string 'PHP' when it is at the start of a line".



                  Obviously, this works if the output format of php -v is consistent across versions/builds.



                  For reference, the whole output was:



                  PHP 7.0.32-0ubuntu0.16.04.1 (cli) ( NTS )
                  Copyright (c) 1997-2017 The PHP Group
                  Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
                  with Zend OPcache v7.0.32-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies





                  share|improve this answer












                  On my system:



                  $> php -v | grep ^PHP | cut -d' ' -f2
                  7.0.32-0ubuntu0.16.04.1


                  as grep PHP matches every PHP string it encounters.



                  The ^PHP means "match only the string 'PHP' when it is at the start of a line".



                  Obviously, this works if the output format of php -v is consistent across versions/builds.



                  For reference, the whole output was:



                  PHP 7.0.32-0ubuntu0.16.04.1 (cli) ( NTS )
                  Copyright (c) 1997-2017 The PHP Group
                  Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
                  with Zend OPcache v7.0.32-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 9 hours ago









                  Mr Shunz

                  2,60211619




                  2,60211619






















                      up vote
                      22
                      down vote













                      Extending Jeff Schaller's answer, skip the pipeline altogether and just ask for the internal constant representation:



                      $ php -r 'echo PHP_VERSION;'
                      7.1.15


                      You can extend this pattern to get more, or less, information:



                      $ php -r 'echo PHP_MAJOR_VERSION;'
                      7


                      See the PHP list of pre-defined constants for all available.



                      Benefit? More performant than pipelines and doesn't rely on a defined output format of php -v.






                      share|improve this answer
























                        up vote
                        22
                        down vote













                        Extending Jeff Schaller's answer, skip the pipeline altogether and just ask for the internal constant representation:



                        $ php -r 'echo PHP_VERSION;'
                        7.1.15


                        You can extend this pattern to get more, or less, information:



                        $ php -r 'echo PHP_MAJOR_VERSION;'
                        7


                        See the PHP list of pre-defined constants for all available.



                        Benefit? More performant than pipelines and doesn't rely on a defined output format of php -v.






                        share|improve this answer






















                          up vote
                          22
                          down vote










                          up vote
                          22
                          down vote









                          Extending Jeff Schaller's answer, skip the pipeline altogether and just ask for the internal constant representation:



                          $ php -r 'echo PHP_VERSION;'
                          7.1.15


                          You can extend this pattern to get more, or less, information:



                          $ php -r 'echo PHP_MAJOR_VERSION;'
                          7


                          See the PHP list of pre-defined constants for all available.



                          Benefit? More performant than pipelines and doesn't rely on a defined output format of php -v.






                          share|improve this answer












                          Extending Jeff Schaller's answer, skip the pipeline altogether and just ask for the internal constant representation:



                          $ php -r 'echo PHP_VERSION;'
                          7.1.15


                          You can extend this pattern to get more, or less, information:



                          $ php -r 'echo PHP_MAJOR_VERSION;'
                          7


                          See the PHP list of pre-defined constants for all available.



                          Benefit? More performant than pipelines and doesn't rely on a defined output format of php -v.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 5 hours ago









                          bishop

                          1,061619




                          1,061619




















                              up vote
                              7
                              down vote













                              There are different ways, I like to use look behind:



                              php -v | grep -Po '(?<=^PHP )[^ ]+'


                              or



                              php -v | grep -Po '(?<=PHP )([0-9.]+)'





                              share|improve this answer




















                              • Thanks Ravexina. The above command worked like a charm.
                                – The One
                                9 hours ago














                              up vote
                              7
                              down vote













                              There are different ways, I like to use look behind:



                              php -v | grep -Po '(?<=^PHP )[^ ]+'


                              or



                              php -v | grep -Po '(?<=PHP )([0-9.]+)'





                              share|improve this answer




















                              • Thanks Ravexina. The above command worked like a charm.
                                – The One
                                9 hours ago












                              up vote
                              7
                              down vote










                              up vote
                              7
                              down vote









                              There are different ways, I like to use look behind:



                              php -v | grep -Po '(?<=^PHP )[^ ]+'


                              or



                              php -v | grep -Po '(?<=PHP )([0-9.]+)'





                              share|improve this answer












                              There are different ways, I like to use look behind:



                              php -v | grep -Po '(?<=^PHP )[^ ]+'


                              or



                              php -v | grep -Po '(?<=PHP )([0-9.]+)'






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 9 hours ago









                              Ravexina

                              1,137719




                              1,137719











                              • Thanks Ravexina. The above command worked like a charm.
                                – The One
                                9 hours ago
















                              • Thanks Ravexina. The above command worked like a charm.
                                – The One
                                9 hours ago















                              Thanks Ravexina. The above command worked like a charm.
                              – The One
                              9 hours ago




                              Thanks Ravexina. The above command worked like a charm.
                              – The One
                              9 hours ago










                              up vote
                              7
                              down vote













                              If you've installed php via the package manager (e.g. RPM or yum), then you can query the version from there:



                              rpm -q --queryformat="%VERSION" php


                              Alternatively, you can ask php to tell you its version directly:



                              php -r 'echo phpversion();'





                              share|improve this answer


























                                up vote
                                7
                                down vote













                                If you've installed php via the package manager (e.g. RPM or yum), then you can query the version from there:



                                rpm -q --queryformat="%VERSION" php


                                Alternatively, you can ask php to tell you its version directly:



                                php -r 'echo phpversion();'





                                share|improve this answer
























                                  up vote
                                  7
                                  down vote










                                  up vote
                                  7
                                  down vote









                                  If you've installed php via the package manager (e.g. RPM or yum), then you can query the version from there:



                                  rpm -q --queryformat="%VERSION" php


                                  Alternatively, you can ask php to tell you its version directly:



                                  php -r 'echo phpversion();'





                                  share|improve this answer














                                  If you've installed php via the package manager (e.g. RPM or yum), then you can query the version from there:



                                  rpm -q --queryformat="%VERSION" php


                                  Alternatively, you can ask php to tell you its version directly:



                                  php -r 'echo phpversion();'






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited 3 hours ago

























                                  answered 5 hours ago









                                  Jeff Schaller

                                  32.9k849110




                                  32.9k849110




















                                      up vote
                                      4
                                      down vote













                                      Since you started with awk, here's an awk solution:



                                      $ php -v | awk '/^PHP/print $2'
                                      7.2.10





                                      share|improve this answer
























                                        up vote
                                        4
                                        down vote













                                        Since you started with awk, here's an awk solution:



                                        $ php -v | awk '/^PHP/print $2'
                                        7.2.10





                                        share|improve this answer






















                                          up vote
                                          4
                                          down vote










                                          up vote
                                          4
                                          down vote









                                          Since you started with awk, here's an awk solution:



                                          $ php -v | awk '/^PHP/print $2'
                                          7.2.10





                                          share|improve this answer












                                          Since you started with awk, here's an awk solution:



                                          $ php -v | awk '/^PHP/print $2'
                                          7.2.10






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered 3 hours ago









                                          terdon♦

                                          123k28233407




                                          123k28233407




















                                              up vote
                                              1
                                              down vote













                                              If you want to do it with just a single function being piped, you can try using sed like this:



                                              php -v | sed -e '/^PHP/!d' -e 's/.* ([0-9]+.[0-9]+.[0-9]+).*$/1/'


                                              First it deletes any line that doesn't begin with PHP, then it clips the version from that line assuming it is the first sequence in the form of x.y.z.



                                              Or, if you want something closer to your original script, simply put ^ in the front of your grep pattern to only look for lines that start with PHP:



                                              php -v | grep ^PHP | awk 'print $2'





                                              share|improve this answer
























                                                up vote
                                                1
                                                down vote













                                                If you want to do it with just a single function being piped, you can try using sed like this:



                                                php -v | sed -e '/^PHP/!d' -e 's/.* ([0-9]+.[0-9]+.[0-9]+).*$/1/'


                                                First it deletes any line that doesn't begin with PHP, then it clips the version from that line assuming it is the first sequence in the form of x.y.z.



                                                Or, if you want something closer to your original script, simply put ^ in the front of your grep pattern to only look for lines that start with PHP:



                                                php -v | grep ^PHP | awk 'print $2'





                                                share|improve this answer






















                                                  up vote
                                                  1
                                                  down vote










                                                  up vote
                                                  1
                                                  down vote









                                                  If you want to do it with just a single function being piped, you can try using sed like this:



                                                  php -v | sed -e '/^PHP/!d' -e 's/.* ([0-9]+.[0-9]+.[0-9]+).*$/1/'


                                                  First it deletes any line that doesn't begin with PHP, then it clips the version from that line assuming it is the first sequence in the form of x.y.z.



                                                  Or, if you want something closer to your original script, simply put ^ in the front of your grep pattern to only look for lines that start with PHP:



                                                  php -v | grep ^PHP | awk 'print $2'





                                                  share|improve this answer












                                                  If you want to do it with just a single function being piped, you can try using sed like this:



                                                  php -v | sed -e '/^PHP/!d' -e 's/.* ([0-9]+.[0-9]+.[0-9]+).*$/1/'


                                                  First it deletes any line that doesn't begin with PHP, then it clips the version from that line assuming it is the first sequence in the form of x.y.z.



                                                  Or, if you want something closer to your original script, simply put ^ in the front of your grep pattern to only look for lines that start with PHP:



                                                  php -v | grep ^PHP | awk 'print $2'






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered 1 hour ago









                                                  Christian Gibbons

                                                  291110




                                                  291110




















                                                      up vote
                                                      0
                                                      down vote













                                                      php -v | awk NR==1'print $2'





                                                      share|improve this answer
























                                                        up vote
                                                        0
                                                        down vote













                                                        php -v | awk NR==1'print $2'





                                                        share|improve this answer






















                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          php -v | awk NR==1'print $2'





                                                          share|improve this answer












                                                          php -v | awk NR==1'print $2'






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered 48 mins ago









                                                          Goro

                                                          4,61752356




                                                          4,61752356



























                                                               

                                                              draft saved


                                                              draft discarded















































                                                               


                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function ()
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471521%2fhow-to-get-only-the-version-number-of-php%23new-answer', 'question_page');

                                                              );

                                                              Post as a guest













































































                                                              Comments

                                                              Popular posts from this blog

                                                              Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                                              What does second last employer means? [closed]

                                                              One-line joke