How to get only the version number of php?
Clash 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?
php version
add a comment |Â
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?
php version
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
add a comment |Â
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?
php version
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
php version
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
.
add a comment |Â
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.]+)'
Thanks Ravexina. The above command worked like a charm.
â The One
9 hours ago
add a comment |Â
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();'
add a comment |Â
up vote
4
down vote
Since you started with awk
, here's an awk
solution:
$ php -v | awk '/^PHP/print $2'
7.2.10
add a comment |Â
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'
add a comment |Â
up vote
0
down vote
php -v | awk NR==1'print $2'
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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 9 hours ago
Mr Shunz
2,60211619
2,60211619
add a comment |Â
add a comment |Â
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
.
add a comment |Â
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
.
add a comment |Â
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
.
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
.
answered 5 hours ago
bishop
1,061619
1,061619
add a comment |Â
add a comment |Â
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.]+)'
Thanks Ravexina. The above command worked like a charm.
â The One
9 hours ago
add a comment |Â
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.]+)'
Thanks Ravexina. The above command worked like a charm.
â The One
9 hours ago
add a comment |Â
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.]+)'
There are different ways, I like to use look behind:
php -v | grep -Po '(?<=^PHP )[^ ]+'
or
php -v | grep -Po '(?<=PHP )([0-9.]+)'
answered 9 hours ago
Ravexina
1,137719
1,137719
Thanks Ravexina. The above command worked like a charm.
â The One
9 hours ago
add a comment |Â
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
add a comment |Â
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();'
add a comment |Â
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();'
add a comment |Â
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();'
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();'
edited 3 hours ago
answered 5 hours ago
Jeff Schaller
32.9k849110
32.9k849110
add a comment |Â
add a comment |Â
up vote
4
down vote
Since you started with awk
, here's an awk
solution:
$ php -v | awk '/^PHP/print $2'
7.2.10
add a comment |Â
up vote
4
down vote
Since you started with awk
, here's an awk
solution:
$ php -v | awk '/^PHP/print $2'
7.2.10
add a comment |Â
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
Since you started with awk
, here's an awk
solution:
$ php -v | awk '/^PHP/print $2'
7.2.10
answered 3 hours ago
terdonâ¦
123k28233407
123k28233407
add a comment |Â
add a comment |Â
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'
add a comment |Â
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'
add a comment |Â
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'
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'
answered 1 hour ago
Christian Gibbons
291110
291110
add a comment |Â
add a comment |Â
up vote
0
down vote
php -v | awk NR==1'print $2'
add a comment |Â
up vote
0
down vote
php -v | awk NR==1'print $2'
add a comment |Â
up vote
0
down vote
up vote
0
down vote
php -v | awk NR==1'print $2'
php -v | awk NR==1'print $2'
answered 48 mins ago
Goro
4,61752356
4,61752356
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-get-only-the-version-number-of-php%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
9 hours ago
Oh, head -1 is very simple and elegent. Thanks man.
â The One
9 hours ago