How to get Ubuntu distribution's full code name?
$ 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?
Solution 1:
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 Tahr
Edit. 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 Tahr
Note 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
Solution 2:
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/*, /}')
Solution 3:
Grep:
$ grep $(lsb_release -rs) /usr/share/python-apt/templates/Ubuntu.info | grep -m 1 "Description: Ubuntu " | cut -d "'" -f2
Trusty Tahr
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:
$ awk -v var=$(lsb_release -rs) '$3~var {print $4" "$5;exit;}' /usr/share/python-apt/templates/Ubuntu.info | cut -d"'" -f2
Trusty Tahr
Explanation:
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.
Solution 4:
The command you are looking for is:
grep -oP '(?<=VERSION\=\"(\d\d)\.(\d\d)\,\ )(.*?)(?="$)' /etc/os-release
This is very ugly and not optimized. I'm sure there should be an easier method and this has some issues.
Solution 5:
Answer relevant for at least ubuntu 16.04 and above:
lsb_release -cs
If for some reason lsb_release
is not available
cat /etc/os-release | grep UBUNTU_CODENAME | cut -d = -f 2