0

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, source is a synonym for .. I updated my answer to cite this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.