Reading from a file and storing it in a variable

Hi folks,
I'm using bash and would like to do the following. I would like to read some values from the file and store it in the variable and use it.

My file is 1.txt and its contents are

VERSION=5.6
UPDATE=4

I would like to read "5.6" and "4" and store it in a variable in shell script. How do I do that?

Thanks,

You can use: eval $(<file)

I don't understand this though. How do I search for patterns in a text file and read the whole line in shell script?

# cat file
VERSION=5.6
UPDATE=4

# cat script.sh
#!/bin/bash -x
eval $(<file)
echo ${VERSION} ${UPDATE}

# /temp/script.sh
+ eval VERSION=5.6 UPDATE=4
++ VERSION=5.6
++ UPDATE=4
+ echo 5.6 4
5.6 4

Can any one help, why my code is not right?

IFS="="
while read key value
do
  $key=$value
done < 1.txt

That's correct, however my previous solution will do exactly the same

eval $(<file)
variables are declared like this 
 key=$value 

however, since they are taken from file, you can use declare (instead of eval , which is dangerous in some cases)

 while IFS== read -r key value do declare $key=$value done 
1 Like