user3898993 user3898993 - 9 days ago 4
Linux Question

How to check latest kernel version by bash

How can I check latest kernel version by bash?
Is there any command to check latest kernel from https://www.kernel.org/ ?

Answer

If you are looking for the latest kernel version on the website and not the one on your system, you can use this command. It will work fine unless they change their page layout later. If they do, in that case, you will have to tweak your command:

[root@slave2 gc]# curl -s https://www.kernel.org/ | grep -A1 'mainline:' | grep -oP '(?<=strong>).*(?=</strong.*)'
            3.16-rc7

It will return you the 'mainline' release. You can search for 'stable' release using the same logic.

Explanation:

-o Option to print only what matches the pattern.

-P Interpret the pattern as a Perl regular expression.

(?=pattern) A zero-width positive look-ahead assertion. To put it in simple words using an example, q(?=u) matches a q that is followed by a u.

(?<=pattern) A zero-width positive look-behind assertion. To put it in simple words using an example, (?<=a)b matches the b (and only the b) in cab, but does not match bed or debt

So, whatever pattern is matched is actually removed from the output and that's how we get the result. :)

You can refer these links for more detail:
http://perldoc.perl.org/perlre.html#Extended-Patterns
http://www.regular-expressions.info/lookaround.html

Comments