how to get value of variable config in bash? [dupl

2020-03-24 05:53发布

I have a linux config file with with format like this:

VARIABLE=5753
VARIABLE2=""
....

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

标签: regex linux bash
3条回答
对你真心纯属浪费
2楼-- · 2020-03-24 06:28
$> cat ./text 
VARIABLE=5753
VARIABLE2=""

With perl regular expression grep could match these value using lookbehind operator.

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

And for VARIABLE:

$> grep --only-matching --perl-regex "(?<=VARIABLE\=).*" ./text
5753
查看更多
放我归山
3楼-- · 2020-03-24 06:34
eval $(grep "^VARIABLE=" configfile)

will select the line and evaluate it in the current bash context, setting the variable value. After doing this, you will have a variable named VARIABLE with value 5753. If no such line exists in the configfile, nothing happens.

查看更多
狗以群分
4楼-- · 2020-03-24 06:34

You could use the source (a.k.a. .) command to load all of the variables in the file into the current shell:

$ source myfile.config

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

$ echo $VARIABLE
5753
查看更多
登录 后发表回答