skip to Main Content

I am new to bash and working on simple script..

I uses this command in my script:

php -i | sed -n '/^Loaded Configuration File/s/.*=> //p'

The output is like this:

/etc/php/7.4/cli/php.ini

I want to strip the version part and save it into a variable, without the dot.

I want to make a variable with only the php version, for example in the result above my variable should be:

version=$"74"

if php version 7.3, The version variable should be 73 .. and so on ..

Is there any way to do it?

2

Answers


  1. If you’re concerned about the command-line version and you want to have the version of it, you can run PHP code by it (-r switch) and directly fetch the information:

    VERSION="$(php -r 'echo PHP_MAJOR_VERSION, PHP_MINOR_VERSION;')"
    printf '%sn' "$VERSION" # 74n
    

    This spares you to pipe into sed (which is not a bad idea in general, but do as little as necessary and PHP knows about its version already).

    Login or Signup to reply.
  2. With php:

    php  -r 'print preg_replace("/[^0-9]/", "", php_ini_loaded_file());'
    

    Output:

    74
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search