slantalpha
slantalpha

Reputation: 585

How to obtain variable values from a text file for use by a bash script?

I have a bash script, and I'd like it to read in the values of variables from a text file.

I'm thinking that in the text file where the values are stored, I'd use a different line for each variable / value pair and use the equals sign.

VARIABLE1NAME=VARIABLE1VALUE
VARIABLE2NAME=VARIABLE2VALUE

I'd then like my bash script to assign the value VARIABLE1VALUE to the variable VARIABLE1NAME, and the same for VARIABLE2VALUE / VARIABLE2NAME.

Upvotes: 0

Views: 39

Answers (1)

Greg Tarsa
Greg Tarsa

Reputation: 1642

Since the syntax of the file is the syntax you would use in the script itself, the source command should do the trick:

source text-file-with-assignments.txt

alternately, you can use . instead of source, but in a case like this, using the full name is more clear.

The documentation can be found in the GNU Bash Reference Manual.

Upvotes: 3

Related Questions