$ 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?
8 Answers
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:
$ . /etc/os-release
$ echo "$VERSION"
14.04, Trusty TahrEdit. If you want to remove the 14.04, part (as asked by terdon), you could:
$ . /etc/os-release
$ read _ UBUNTU_VERSION_NAME <<< "$VERSION"
$ echo "$UBUNTU_VERSION_NAME"
Trusty TahrNote that this is a bit clunky, since on other distributions, the VERSION field can have different format. E.g., on my debian,
$ . /etc/os-release
$ read _ UBUNTU_VERSION_NAME <<< "$VERSION"
$ echo "$UBUNTU_VERSION_NAME"
(wheezy)Then, you could imagine something like this (in a script):
#!/bin/bash
if [[ -r /etc/os-release ]]; then . /etc/os-release if [[ $ID = ubuntu ]]; then read _ UBUNTU_VERSION_NAME <<< "$VERSION" echo "Running Ubuntu $UBUNTU_VERSION_NAME" else echo "Not running an Ubuntu distribution. ID=$ID, VERSION=$VERSION" fi
else echo "Not running a distribution with /etc/os-release available"
fi 5 My variant on what's already offered:
. /etc/os-release; echo ${VERSION/*, /}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:
bash <(cat /etc/os-release; echo 'echo ${VERSION/*, /}') 6 Grep:
$ grep $(lsb_release -rs) /usr/share/python-apt/templates/Ubuntu.info | grep -m 1 "Description: Ubuntu " | cut -d "'" -f2
Trusty TahrExplanation:
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-mflag) which contains the stringDescription: Ubuntu.cut -d "'" -f2-> prints the field number 2 according to the delimiter single quote'
Awk:
$ awk -v var=$(lsb_release -rs) '$3~var {print $4" "$5;exit;}' /usr/share/python-apt/templates/Ubuntu.info | cut -d"'" -f2
Trusty TahrExplanation:
Declaring and assigning Variable in awk is done through -v parameter.So the value of lsb_release -rs command is assigned to the variable var which inturn helps to print field 4 ,field 5 from the lines contain the string 14.04 and exists if its found an one.Finally the cut command helps to remove the single quotes.
The command you are looking for is:
grep -oP '(?<=VERSION\=\"(\d\d)\.(\d\d)\,\ )(.*?)(?="$)' /etc/os-releaseThis is very ugly and not optimized. I'm sure there should be an easier method and this has some issues.
0Answer relevant for at least ubuntu 16.04 and above:
lsb_release -csIf for some reason lsb_release is not available
cat /etc/os-release | grep UBUNTU_CODENAME | cut -d = -f 2 1 . /etc/os-release
echo $VERSION 1 Here are some more choices. They all parse the /etc/os-release file which, on my 13.10, looks like this:
NAME="Ubuntu"
VERSION="13.10, Saucy Salamander"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 13.10"
VERSION_ID="13.10"
HOME_URL=""
SUPPORT_URL=""
BUG_REPORT_URL=""All of the solutions below will parse the second line to produce Saucy Salamander.
grepgrep -oP 'VERSION=.* \K\w* \w*' /etc/os-releaseThe
-omeans "print only the matching part of the line" and the-Penables Perl Compatible Regular Expressions. This lets us use\Kwhich discards whatever was matched up to that point, which combined with-omeans "print only what matches after the\K. So, the actual regular expression used will match the last two words of a line that containsVERSION=.awkawk -F'[" ]' '/VERSION=/{print $3,$4}' /etc/os-releaseSetting the fields separator to
"andspacemeans that the 3d and 4rth fields of the line containingVERSION=are the string we're after.sedsed -nr '/VERSION=/{s/.* (\w* \w*)"/\1/;p}' /etc/os-releaseThe
-nswitch suppresses normal output, no lines will be printed. The-rallows extended regular expressions. Then, on lines that matchVERSION=, we will delete everything except the last two words. Thepat the end means print.perlperl -lne '/VERSION=.*\b(\w+ \w+)/ && print $1' /etc/os-releaseThe
-nmeans 'process every input line with the script given by-e". The-ladds a newline character to every print call (and some other stuff which is not relevevant here). The regular expression matches the last two words (\bis a word boundary) and prints them if the line containsVERSION=.coreutils
grep VERSION= /etc/os-release | cut -d ' ' -f 2- | tr -d '"'Here, we just
grepthe relevant line and usecutsetting the field separator tospace(-d ' ') and printing everything from the 2nd field to the end of the line. Thetr -dcommand will delete the"from the end.Pure shell (shamelessly stealing @gniourf_gniourf's clever
sourceidea):. /etc/os-release && echo ${VERSION//[0-9,. ]/ }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:
grep 'VERSION' /etc/os-release | grep -oP "[a-zA-Z]+ [a-zA-Z]+"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.