How to get the value of a config variable in bash?

I have a linux configuration file with this format:

VARIABLE=5753 VARIABLE2="" .... 

How can I get fe value of VARIABLE2 using standard linux tools or regular expressions? (I need to parse the directory path from the file). Thanks in advance.

+4
source share
3 answers
 $> cat ./text VARIABLE=5753 VARIABLE2="" 

With perl , the grep regular expression can match this value with the lookbehind operator.

 $> grep --only-matching --perl-regex "(?<=VARIABLE2\=).*" ./text "" 

And for VARIABLE :

 $> grep --only-matching --perl-regex "(?<=VARIABLE\=).*" ./text 5753 
+4
source
 eval $(grep "^VARIABLE=" configfile) 

will select a string and evaluate it in the current bash context by setting the value of the variable. After that, you will have a variable named VARIABLE with a value of 5753 . If such a line does not exist in the configuration file, nothing happens.

+5
source

You can use the source (aka . ) Command to load all the variables into a file in the current shell:

 $ source myfile.config 

Now you have access to the values ​​of the variables defined inside the file:

 $ echo $VARIABLE 5753 
+4
source

Source: https://habr.com/ru/post/1399621/


All Articles