skip to Main Content

When running command to get php version php -v I get that long output which I want to reduce so the output is i.e.

PHP 7.3.27-9 

instead of

PHP 7.3.27-9+ubuntu18.04.1+deb.sury.org+1 (cli) 
(built: Feb 23 2021 15:10:08) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.27, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.27-9+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

This would mean leaving everything after sign - or +.

Currently, I have this:
php -v | grep -e '^PHP [5.*,7.*,8.*]$*'

Which gives output:

PHP 7.3.27-9+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Feb 23 2021 15:10:08) ( NTS )

I would like to leave out everything after PHP 7.3.27.

3

Answers


  1. Use parameter expansion.

    v=$(php -v)
    echo "${v%%-*}"
    

    % removes the pattern on the right hand side, doubling the percent sign makes it greedy, i.e. it removes the longest part possible (i.e. starting at the first dash from the left).

    Login or Signup to reply.
  2. Try awk ?

    php -v | awk -F'-' '/^PHP/{print $1}'
    
    Login or Signup to reply.
  3. Use php, it’s there.

    php -r 'print(PHP_VERSION);'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search