frans
frans

Reputation: 9758

bash/shell: easiest way to get kernel name components on command line

On my Fedora machine I sometimes need to find out certain components of the kernel name, e.g.

VERSION=3.18.9-200.fc21
VERSION_ARCH=3.18.9-200.fc21.x86_64
SHORT_VERSION=3.18
DIST_VERSION=fc21
EXTRAVERSION = -200.fc21.x86_64

I know uname -a/-r/-m but these give me not all the components I need.

Of course I can just disassemble uname -r e.g.

KERNEL_VERSION_ARCH=$(uname -r)
KERNEL_VERSION=$(uname -r | cut -d '.' -f 1-4)
KERNEL_SHORT_VERSION=$(uname -r | cut -d '.' -f 1-2)
KERNEL_DIST_VERSION=$(uname -r | cut -d '.' -f 4)
EXTRAVERSION="-$(uname -r | cut -d '-' -f 2)"

But this seems very cumbersome and not future-safe to me.

Question: is there an elegant way (i.e. more readable and distribution aware) to get all kernel version/name components I need?

Nice would be s.th. like

kernel-ver -f "%M.%m.%p-%e.%a"
3.19.4-200.fc21.x86_64
kernel-ver -f "%M.%m"
3.19
kernel-ver -f "%d"
fc21

Upvotes: 2

Views: 1203

Answers (1)

chaos
chaos

Reputation: 9282

Of course the uname -r part would need a bit sed/awk/grep magic. But there are some other options you can try:

  • cat /etc/os-release
  • cat /etc/lsb-release
  • Since it's fedora you can try: cat /etc/fedora-release
  • lsb_release -a is also worth a try.
  • cat /proc/version, but that nearly the same output as uname -a

In the files /etc/*-release the format is already VARIABLE=value, so you could source the file directly and access the variables later:

$ source /etc/os-release
$ echo $ID
fedora

To sum this up a command that should work on every system that combines the above ideas:

cat /etc/*_ver* /etc/*-rel* 2>/dev/null

Upvotes: 1

Related Questions