I have a PHP script which exports a pipe-delimited list of variables like this to STDOUT:
DB_HOST|localhost
DB_DATABASE|mydb
DB_USER|postgres
...
What I want to do is read them into a Bash script and set them as shell variables to be used by any programs called from inside the shell script, i.e.
psql -U $DB_USER -h $DB_HOST -d $DB_DATABASE <<END_OF_SQL
code...
END_OF_SQL
Here's what I did, which isn't working:
#!/bin/bash
# We don't need an eval here but just to confirm that it works with eval.
eval export FOO_DOCROOT=/web/gallery
php get_env.php | while read X
do
LINE=(`echo $X | tr "\|" "\n"`)
V="${LINE[0]}=${LINE[1]}" # Outputs a string, i,e. "FOO_DBHOST=localhost".
echo "V=$V"
echo $V
# This has no effect.
eval export $V
done;
echo Check the environment
echo /bin/env:
env
# env has FOO_DOCROOT set correctly
# but none of the evals in the DO/DONE loop are set.
I've confirmed that the set env strings don't have any whitespace or special characters. But no matter what permutation of arguments I pass to eval the variables don't get set.
Any ideas? Is there a security block with creating new shell variables programmatically in bash?