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
Use parameter expansion.
%
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).Try
awk
?Use
php
, it’s there.