I want to check the version number of an application only, eg gcc
for illustrative purposes:
#!/bin/sh
VERSION=`gcc --version | head -n 1 | grep -o " [0-9][0-9]"`
echo $VERSION
This yields the string "11 11" however, where I was expecting just the substring "11":
$ ./version.sh
11 11
How do I change the grep statement to have only 11
?
what is the actual output of the 'gcc --version' command ?
on one of my hosts , for example:
gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
munke@Z800:$ VERSION="$(gcc --version | head -n 1|awk '{print $NF}')"
munke@Z800:$ echo $VERSION
9.4.0
Thanks for your solution. I was looking to use grep, given it works on regular expressions.
$ gcc --version
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Obviously grep -o
prints all matches in the line.
A more exact match would include an extra boundary, which is printed unless it's in a look-behind that requires -P
option for grep...
Perl is easy!
VERSION=$(
gcc --version | perl -lne '$. == 1 and / (\d+)/ and print $1'
)
or bash
#!/bin/bash
[[ $( gcc --version ) =~ " "([0-9]+) ]] && VERSION=${BASH_REMATCH[1]}
1 Like
both versions match the 9
of Ubuntu 9 ...
.
Yes, and that's apparently the gcc version.
I asked MS-Copilot and it suggested
gcc -dumpversion
Indeed man gcc
describes -dumpversion
and -dumpfullversion
!
2 Likes