0

I am making a system info script which will output what version of CentOS a machine is running.

Currently, I have the following:

#!/bin/bash
distro=$(cat /etc/redhat-release)

echo "OS Version: $distro" 

For the output, I get the following:

OS Version: CentOS Linux release 7.9.2009 (Core)

I would like to know how I can remove the (Core) section of the output.

2
  • Can you use uname? Otherwise you can use sed to remove some part of the string. Commented Jul 23, 2024 at 11:04
  • Hello @pfnuesel, Unfortunately uname doesn't give me the Linux Distro info. I am reading up on the sed command, but still trying to figure out how to use it. Commented Jul 23, 2024 at 11:17

1 Answer 1

1

Using the sed command in the following manner seems to have done what I need:

sed 's/[(].*//' /etc/redhat-release

Output:

CentOS Linux release 7.9.2009

Explanation:

  • s/old/new/ is the substitution operator and will replace old with new.
  • [(].* the [ ] denote a character class. Here, we have a character class of one character, (. The .* means "0 or more characters".
  • //: here we are leaving the right hand side of the substitution operator empty.

So with the above command, I am replacing everything from the first bracket ( until the end of the line with nothing, effectively deleting it.

The following post helped me: Remove the first part of a string using sed.

3
  • @terdon thanks a lot for your input and for updating my answer. Your information has been very helpful, especially on my uppercase variable names, for some reason I thought variables in Bash were supposed to be like that. Commented Jul 23, 2024 at 12:47
  • 1
    Yeah, it's a very common misconception caused by the fact that global environment variables (e.g. $PATH or $SHELL or $HOME) are capitalized. But that's precisely why it's a bad idea to have your own, local shell variables in caps because that can lead to naming collisions and really hard to debug bugs. Commented Jul 23, 2024 at 12:55
  • @terdon I am aware of global environment variables, and you're right they are always in uppercase. I'm shocked it never occurred to me, haha. Thank you once again for your insightful information. If you have any good sources for me to further my knowledge on shell scripting, I would be grateful. Commented Jul 23, 2024 at 13:02

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.