How to grep only php version from php -v?
Clash 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?
centos awk php version
add a comment |Â
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?
centos awk php version
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
add a comment |Â
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?
centos awk php version
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
centos awk php version
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
add a comment |Â
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
add a comment |Â
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
Thanks man. It worked.
– The One
3 hours ago
add a comment |Â
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.]+)'
Thanks Ravexina. The above command worked like a charm.
– The One
3 hours ago
add a comment |Â
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();'
add a comment |Â
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
Thanks man. It worked.
– The One
3 hours ago
add a comment |Â
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
Thanks man. It worked.
– The One
3 hours ago
add a comment |Â
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
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
answered 3 hours ago
Mr Shunz
2,54111619
2,54111619
Thanks man. It worked.
– The One
3 hours ago
add a comment |Â
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
add a comment |Â
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.]+)'
Thanks Ravexina. The above command worked like a charm.
– The One
3 hours ago
add a comment |Â
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.]+)'
Thanks Ravexina. The above command worked like a charm.
– The One
3 hours ago
add a comment |Â
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.]+)'
There are different ways, I like to use look behind:
php -v | grep -Po '(?<=^PHP )[^ ]+'
or
php -v | grep -Po '(?<=PHP )([0-9.]+)'
answered 3 hours ago


Ravexina
997719
997719
Thanks Ravexina. The above command worked like a charm.
– The One
3 hours ago
add a comment |Â
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
add a comment |Â
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();'
add a comment |Â
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();'
add a comment |Â
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();'
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();'
answered 17 mins ago


Jeff Schaller
32.8k849110
32.8k849110
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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