How can I read parts of a command output (after a specific character) into a bash variable?

So, I have a command that returns output in the following form

># command
VAR1=ABC
VAR2=DEF
VAR3=123

      

And I want to read VAR1 and VAR3 from this command into a shell script. So logically I run the following two commands

># command | grep VAR1
VAR1=ABC
># command | grep VAR3
VAR3=123

      

How can I only capture the part that appears after the first equal sign? (So ​​"$ {VAR1}" = "ABC" and "$ {VAR3}" = "123"). It should also be noted that in the valley of any variable there may be a different equal sign, so I need to keep everything after the first equal sign, including the following

+3


source to share


2 answers


You can use awk:

command | awk -F = '/VAR1|VAR3/{print substr($0, index($0, "=")+1)}'
ABC
123

      



Decay:

/VAR1|VAR3/    # searches for VAR1 OR VAR3
index($0, "=") # search for = in the record
substr         # gets the substring after first =

      

+1


source


This should work:



># command | grep -oP '(?<=VAR1=).*'
ABC
># command | grep -oP '(?<=VAR3=).*'
123

      

0


source







All Articles