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,
danmero
2
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?
danmero
4
# 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
rdcwayx
5
Can any one help, why my code is not right?
IFS="="
while read key value
do
$key=$value
done < 1.txt
danmero
6
That's correct, however my previous solution will do exactly the same
eval $(<file)
kurumi
7
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