1

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).

3
  • 6
    what's wrong with using php --version? Commented Jul 26, 2019 at 13:49
  • I need just the 7.2 as in php_dev=php$php_version-dev, apt-get -y install $php_dev. 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 Commented Jul 26, 2019 at 13:53
  • 1
    You have to be careful as the CLI and web versions may be different. Not sure if this affects your circumstances but can be significant. Commented Jul 26, 2019 at 14:08

3 Answers 3

2

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*'
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

This way uses CLI

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

OR

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.