SQlBeginner
SQlBeginner

Reputation: 75

Reading values from config file in linux

I am working in linux and have a config file with a lot of single lines formatted like this:

Variable1=Value1
Variable2=Value2
Variable3=Value3

I need something I can run on command line that will echo the value for the respective variable. I have been playing with sed all day, but having a heck of a time. I'm not sure if that's even the best way. Any help would be super.

Upvotes: 2

Views: 3919

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799230

Search for the variable name and the equal sign, remove them, and print the result.

sed --quiet 's/^Variable1=//p' config.txt

Output:

Value1

With p flag, the s command only prints line(s) where substitution was made. With the --quiet option, no other lines are printed.

Upvotes: 2

iamauser
iamauser

Reputation: 11489

$ cat a.sh
Variable1=Value1
Variable2=Value2
Variable3=Value3 

$ source a.sh
$ echo "$Variable1"
Value1

Note, source will overwrite the value of Variable1 for the current shell.

Upvotes: 2

Related Questions