Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing variables from config file in Bash

Having the following content in a file:

VARIABLE1="Value1" VARIABLE2="Value2" VARIABLE3="Value3" 

I need a script that outputs the following:

Content of VARIABLE1 is Value1 Content of VARIABLE2 is Value2 Content of VARIABLE3 is Value3 

Any ideas?

like image 392
KillDash9 Avatar asked May 15 '13 17:05

KillDash9


People also ask

How read config file in Linux?

conf file in Linux, first locate it in the file system. Most often, the dhclient. conf file will be located in the /etc or /etc/DHCP directory. Once you find the file, open it with your favorite command-line editor.

Where is bash config file?

The /etc/bashrc file might be referred to in /etc/profile or in individual user shell initialization files. The source contains sample bashrc files, or you might find a copy in /usr/share/doc/bash-2.05b/startup-files.


1 Answers

Since your config file is a valid shell script, you can source it into your current shell:

. config_file echo "Content of VARIABLE1 is $VARIABLE1" echo "Content of VARIABLE2 is $VARIABLE2" echo "Content of VARIABLE3 is $VARIABLE3" 

Slightly DRYer, but trickier

. config_file for var in VARIABLE1 VARIABLE2 VARIABLE3; do     echo "Content of $var is ${!var}" done 
like image 144
glenn jackman Avatar answered Sep 18 '22 19:09

glenn jackman