CarComp
CarComp

Reputation: 2016

Bash script to get php version

I would like to get the current numerical value for php in the php.ini path. I understand that php -v gets me a bunch of info, but I just need the "7.2" or whatever the current version is from the php.ini path.

Edit: I'm building an automation script and if the version changes, I need to be able to know what was installed when I ran apt-get install php

This script gets me the line i care about:

php --ini | grep Loaded | cut -d' ' -f12

The result (as of today) is

/etc/php/7.2/cli/php.ini

Whats the best in bash way to echo "7.2" assuming that the /etc/php will not change (its unlikely based on the history of where php installs using apt-get)

I'm open to other methods that don't involve php --ini, I just need the 7.2 (or whatever that path value may morph into).

Upvotes: 1

Views: 1344

Answers (3)

Sammitch
Sammitch

Reputation: 32232

If you're building your automations with apt, then you should be relying on apt and/or dpkg to get the relevant information.

dpkg -l 'php*' | grep ^ii

For completenes, yum/dnf/rpm:

rpm -qa 'php*'

Upvotes: 2

potiev
potiev

Reputation: 586

This way uses CLI

php -r "echo substr(phpversion(),0,3);"

OR

php -r "echo implode('.', array_slice(explode('.', PHP_VERSION),0,2));"

Upvotes: 0

CarComp
CarComp

Reputation: 2016

As soon as I asked, I realized I could just adjust the delimiter and look for the 4th item.

php --ini | grep Loaded | cut -d'/' -f4

Upvotes: 0

Related Questions