How to get Ubuntu distribution’s full code name?
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?
It seems that there’s a convergent process towards the sourcing of the file /etc/os-release . Maybe you should specify what you mean by How do I get the full codename(trusty tahr) of my installed Ubuntu system?. Do you only want to echo it on the terminal, or do you need it assigned to a variable? Is this going to be used on some non- systems?
9 Answers 9
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
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
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
There are many and yours is a particularly good one! Comments that are irrelevant to the actual answer can be deleted without warning.
the embedded variable and value in os-release currently is defined to something like this: VERSION="18.04.2 LTS (Bionic Beaver)"
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:
Why the downvote? it's a good answer (though with nothing conceptually different from others given before it).
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 string Description: Ubuntu .
- cut -d "'" -f2 -> prints the field number 2 according to the delimiter single quote '
Awk:
$ awk -v var=$(lsb_release -rs) '$3~var ' /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.
it seems your solutions are based upon some python module for apt. that would mean a dependency on the file location and also on its formatting. also it only works if lsb_release with those options does provide a string that will match for the initial search operation. - nevertheless pointing out to those second source(!) to let others know about can in some special case be worth a million.
the grep variant might be able to be coded even shorter (with maybe a slight deviation on the exact search term - might not have practical relevance) - try this one: grep -m 1 "Description: Ubuntu lsb_release -rs " /usr/share/python-apt/templates/Ubuntu.info | cut -d \' -f 2
oops, left-ticked quotes vanished as mini-Markdown syntax understood these as code highlighting request. you will have to undo this for the proposal to work.
Answer relevant for at least ubuntu 16.04 and above:
If for some reason lsb_release is not available
cat /etc/os-release | grep UBUNTU_CODENAME | cut -d = -f 2
even if i see some sympathy to your second way of parsing (instead of sourcing) the file in question - neither of the two options returns the full code name - only the first half is retrieved. xenial instead of xenial xerus, bionic instead of bionic beaver. sorry - that was not the question here.
The command you are looking for is:
grep -oP '(? )" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter " data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share )" title="">Improve this answer )">edited Apr 19, 2014 at 6:23 answered Apr 19, 2014 at 4:38 BraiamBraiam 66.8k 30 gold badges 177 silver badges 264 bronze badges