I am writing a script that I am trying to get the linux distribution. I have the following code but it is returning an empty string. I am sure I am missing something
OS=`cat /etc/os-release | grep -sw NAME`
NEWOS=$OS | `cut -d \" -f2`
echo $NEWOS
I am writing a script that I am trying to get the linux distribution. I have the following code but it is returning an empty string. I am sure I am missing something
OS=`cat /etc/os-release | grep -sw NAME`
NEWOS=$OS | `cut -d \" -f2`
echo $NEWOS
To complement the existing, helpful answers with an alternative way to solve the problem, using a single
awk
command:Note that I've changed the case of the variable from
OS
toos
, because it's better not to use all-uppercase shell-variable names in order to avoid conflicts with environment variables and special shell variables.You have two problems.
Typing just
$OS
doesn't output anything to stdout (which is piped tocut
).Your second problem is that you put
cut
in backticks. This means that you're trying to run the output of an empty cut.The solution is to write
$(echo "$OS" | cut -d \" -f2)
Like Honesty's answer says, you have two problems. Adding to his answer, I'd also like to address the use of backticks in your code.
The command substitution can be done in two ways one is using
$(command)
and the other is`command`
. Both work the same, but the$(command)
form is the modern way and has more clarity and readability.The following code uses the modern way:
Output on my computer: