Read name value pairs from yq
and use bash printf -v
to initialize variables.
A little over the top, but a nice way to initialize a script.
Note that adding variables does not change the while loop.
file.sh
#!/bin/bash
while read -r key val; do
printf -v "$key" "$val"
done < <(yq '.variables[] | key + " " + .' data.yml)
echo "Details are $name: $pass - Total value $count"
echo 'Some extra variables:'
echo "\$address: $address, \$phone: $phone, \$url: $url"
data.yml
variables:
count: "100"
name: "sss"
pass: "123"
address: "42 Terrapin Station"
phone: "999-999-9999"
url: "http://www.example.com"
output
Details are sss: 123 - Total value 100
Some extra variables:
$address: 42 Terrapin Station, $phone: 999-999-9999, $url: http://www.example.com