How to grep only php version from php -v?

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











up vote
1
down vote

favorite












I want to grep 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
    4 hours ago










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














up vote
1
down vote

favorite












I want to grep 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
    4 hours ago










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












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to grep 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 grep 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?







centos awk php version






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago









Jeff Schaller

32.8k849110




32.8k849110










asked 4 hours ago









The One

1,04661528




1,04661528







  • 1




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










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












  • 1




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










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







1




1




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




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












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




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










3 Answers
3






active

oldest

votes

















up vote
2
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




















  • Thanks man. It worked.
    – The One
    3 hours ago

















up vote
2
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
    3 hours ago

















up vote
1
down vote













If you've install php via the package manage (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




















    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-grep-only-php-version-from-php-v%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    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




















    • Thanks man. It worked.
      – The One
      3 hours ago














    up vote
    2
    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




















    • Thanks man. It worked.
      – The One
      3 hours ago












    up vote
    2
    down vote



    accepted







    up vote
    2
    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 3 hours ago









    Mr Shunz

    2,54111619




    2,54111619











    • Thanks man. It worked.
      – The One
      3 hours ago
















    • Thanks man. It worked.
      – The One
      3 hours ago















    Thanks man. It worked.
    – The One
    3 hours ago




    Thanks man. It worked.
    – The One
    3 hours ago












    up vote
    2
    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
      3 hours ago














    up vote
    2
    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
      3 hours ago












    up vote
    2
    down vote










    up vote
    2
    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 3 hours ago









    Ravexina

    997719




    997719











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
















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















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




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










    up vote
    1
    down vote













    If you've install php via the package manage (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
      1
      down vote













      If you've install php via the package manage (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
        1
        down vote










        up vote
        1
        down vote









        If you've install php via the package manage (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 install php via the package manage (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










        answered 17 mins ago









        Jeff Schaller

        32.8k849110




        32.8k849110



























             

            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-grep-only-php-version-from-php-v%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery