Search code examples
bashparameter-expansion

Bash bad substitution with subshell


I am trying to get the Distro name without the quotation marks.

cat /etc/*-release | grep "NAME=.*" -o | cut -d "=" -f2 | head -n1

Returns: "CentOS Linux"

Now, using shell substitution, I tried to remove the quotation marks my placing the command in a sub-shell:

echo ${$(cat /etc/*-release | grep "NAME=.*" -o | cut -d "=" -f2 | head -n1)/\"}

I was thinking I could escape the quote using \" and then omitting the last / to simply delete the quotes.

EDIT: I know this could be done with awk, but I don't know how.

EDIT: Output of cat /etc/*-release:

CentOS Linux release 7.2.1511 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

CentOS Linux release 7.2.1511 (Core)
CentOS Linux release 7.2.1511 (Core)

Expected output: CentOS Linux


Solution

  • Using awk, getting the OS name following the NAME variable.

    awk -F"=" '$1=="NAME"{gsub(/"/, "", $2);print $2; exit}' /etc/os-release 
    CentOS Linux