0

I want to get "PRETTY_NAME" of my system from release information file. I used this command in my bash file to get it:

SYS_VERSION=$(cat /etc/*-release | egrep  "^PRETTY_NAME=(.*[a-zA-Z]+.*)$")

It returns the whole line:

PRETTY_NAME="Ubuntu 16.04.3 LTS"

But I just want the characters inside the double quotation.

2

Use sed:

sed '/^PRETTY_NAME=/!d;s///;s/"//g' /etc/*-release

The first command deletes all lines that don't start with PRETTY_NAME=, the second remove the said pattern (empty pattern means use last pattern), the third removes the quotes.

3

Using lsb_release:

$ lsb_release -ds
Ubuntu 17.04

or, in a script,

sys_descr=$( lsb_release -ds )
printf 'This is a "%s" system\n' "$sys_descr"

lsb_release parses the /etc/os-release file. The -d flag will give you the "description" of the system, which is exactly what PRETTY_NAME in /etc/os-release supplies. The -s flag will give you output without any header.

See the manuals for lsb_release and os-release.

2

With single sed expression:

sed -n 's/PRETTY_NAME="\(.*\)"/\1/p' /etc/*release

The output:

Ubuntu 16.04.3 LTS

In your case it would look like:

SYS_VERSION=$(sed -n 's/PRETTY_NAME="\(.*\)"/\1/p' /etc/*release)
1

you can use awk -F '"' '{print $2}'

  • 1
    This doesn't give the string inside the double quotes, but also the quotes themselves. Better use " as delimiter. – Philippos Aug 16 '17 at 12:32
  • @Philippos you where right thanks for your note – smhquery Aug 16 '17 at 12:36
1

You can pipe your command into cut -d '"' -f2:

SYS_VERSION=$(cat /etc/*-release | egrep  "^PRETTY_NAME=(.*[a-zA-Z]+.*)$" | cut -d '"' -f2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.