I have some environment variables declared in a YAML file like:
runtime: python37
env_variables:
API_URL: 'https://fake.api.com/'
API_USERNAME: '[email protected]'
API_PASSWORD: 'Passwooord'
I would like to export these to environment variables with a script, I can echo the correct syntax but I'm still unable to export them.
sed -nr '/env_variables:/,$ s/ ([A-Z_]+): (.*)/1円=2円/ p' app.yaml | while read assign; do echo $assign; done
This is different that this as in my case the variable name is passed trough the pipe as well.
1 Answer 1
Assuming the sed
command correctly outputs lines of the form var=value
, you can do:
while read assign; do
export "$assign";
done < <(sed -nr '/env_variables:/,$ s/ ([A-Z_]+): (.*)/1円=2円/ p' app.yaml)
Or, if you don't need to export, and the input is reasonably safe (no shell syntax),
. <(sed -nr '/env_variables:/,$ s/ ([A-Z_]+): (.*)/1円=2円/ p' app.yaml)
-
Thanks, the second works like a charm! The first works somehow, keeping quotes in the value of the variable, e.g.
echo $API_PASSWORD
prints'Passwooord'
neurino– neurino2019年09月05日 13:55:52 +00:00Commented Sep 5, 2019 at 13:55 -
The
sed
command needs to eliminate the quotes there (the shell doesn't interpret quotes after variable expansion) for the first.muru– muru2019年09月05日 13:57:09 +00:00Commented Sep 5, 2019 at 13:57
read a b < <(echo 1 2 3 4 5)
wherea
andb
are after the pipe. Could you please post a working case with my example?export "$name=$value"
,printf -v "$name" "%s" "$value"
,read "$name" <<<"$value"
, ... (Pick one)