Need bash shell script for reading name value pairs from a file

Hugh Darling picture Hugh Darling · Feb 14, 2011 · Viewed 45.4k times · Source

I have a file like

name1=value1
name2=value2

I need to read this file using shell script and set variables

$name1=value1
$name2=value2

Please provide a script that can do this.

I tried the first answer below, i.e. sourcing the properties file but I'm getting a problem if the value contains spaces. It gets interpreted as a new command after the space. How can I get it to work in the presence of spaces?

Answer

Joachim Sauer picture Joachim Sauer · Feb 14, 2011

If all lines in the input file are of this format, then simply sourcing it will set the variables:

source nameOfFileWithKeyValuePairs

or

. nameOfFileWithKeyValuePairs