How to read multiple strings in a file as a differ

2019-08-08 12:12发布

问题:

I have a conf file content like below.

file.conf:

key name
value pswd

Now I want write a script to having two variable like above

script.sh

key="name"      #key as variable
value="pswd"    #value as variable

. /path/to/file.conf is giving me :- key & value command not found error

回答1:

You can use declare:

while read k v; do
   declare $k="$v"
done < file.conf

Then test these values:

declare -p key
declare -- key="name"

declare -p value
declare -- value="pswd"