$ lsb_release -c
Codename: trusty
$ cat /etc/issue
Ubuntu 14.04 LTS \n \l
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04 LTS"
Output's of the above commands shows only the partial code name (ie, trusty
). How do I get the full codename (trusty tahr) of my installed Ubuntu system?
Using no external tools:
You can just source (the source command is a dot
.
) the/etc/os-release
and you'll have access to all the variables defined there:Edit. If you want to remove the
14.04,
part (as asked by terdon), you could:Note that this is a bit clunky, since on other distributions, the
VERSION
field can have different format. E.g., on my debian,Then, you could imagine something like this (in a script):
My variant on what's already offered:
The shortest, Bashiest answer to date.
If you don't care to load
/etc/os-release
's contents into your current environment, you can fake bash into thinking it's loading a script fairly easily:Grep:
Explanation:
lsb_release -rs
-> Prints your installed Ubuntu version.grep $(lsb_release -rs) /usr/share/python-apt/templates/Ubuntu.info
-> Grab all the lines which contains your release version, in my case it's 14.04.grep -m 1 "Description: Ubuntu "
-> Again grabs only the matched first line(because of-m
flag) which contains the stringDescription: Ubuntu
.cut -d "'" -f2
-> prints the field number 2 according to the delimiter single quote'
Awk:
Explanation:
Declaring and assigning Variable in awk is done through
-v
parameter.So the value oflsb_release -rs
command is assigned to the variablevar
which inturn helps to print field 4 ,field 5 from the lines contain the string14.04
and exists if its found an one.Finally thecut
command helps to remove the single quotes.The command you are looking for is:
This is very ugly and not optimized. I'm sure there should be an easier method and this has some issues.
Answer relevant for at least ubuntu 16.04 and above:
If for some reason
lsb_release
is not availableHere are some more choices. They all parse the
/etc/os-release
file which, on my 13.10, looks like this:All of the solutions below will parse the second line to produce
Saucy Salamander
.grep
The
-o
means "print only the matching part of the line" and the-P
enables Perl Compatible Regular Expressions. This lets us use\K
which discards whatever was matched up to that point, which combined with-o
means "print only what matches after the\K
. So, the actual regular expression used will match the last two words of a line that containsVERSION=
.awk
Setting the fields separator to
"
andspace
means that the 3d and 4rth fields of the line containingVERSION=
are the string we're after.sed
The
-n
switch suppresses normal output, no lines will be printed. The-r
allows extended regular expressions. Then, on lines that matchVERSION=
, we will delete everything except the last two words. Thep
at the end means print.perl
The
-n
means 'process every input line with the script given by-e
". The-l
adds a newline character to every print call (and some other stuff which is not relevevant here). The regular expression matches the last two words (\b
is a word boundary) and prints them if the line containsVERSION=
.coreutils
Here, we just
grep
the relevant line and usecut
setting the field separator tospace
(-d ' '
) and printing everything from the 2nd field to the end of the line. Thetr -d
command will delete the"
from the end.Pure shell (shamelessly stealing @gniourf_gniourf's clever
source
idea):The
.
sources the file which makes the variables available to the shell and I use bash's string manipulation capabilities to remove the version numbers.Using regex you could do this:
Explanation:
This searches the line with
VERSION
in the file/etc/os-release
. Then it finds 2 successive words seperated by a space (Perl regex). The -o flag keeps only what matches the search.You have to use
[a-zA-Z]
instead of `w` to avoid including digits.