0

I have these parameter in postgres-config.ini file:

PGUSER=usr
PGPASSWORD=pass
PGDATABASE=myDB
PGHOST=localhost
PGPORT=5432

I want to parse it and connect to database using shell script (sh file). I use source and grep command like this:

source <( grep = postgres-config.ini )

However, when I tried to print the values using echo $PGHOST it not printing anything.

Can anyone help me what I am doing wrong?

Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
aga.91
  • 37
  • 8

1 Answers1

0

Your source from a process substitution works fine for me from the command line:

$ cat foo.sh
PGUSER=usr
PGPASSWORD=pass
PGDATABASE=myDB
PGHOST=localhost
PGPORT=5432
$ source <(grep = foo.sh)
$ echo $PGHOST
localhost

Note that the shell variables aren't exported, so they won't be available in subprocesses. If you need them in subprocesses, try:

source <(sed '/=/!d;s/^/export /' foo.sh)

which uses sed to add an export command to lines which match =.

Also, where are you running the source from? If it is in a script, remember that the variables will disappear when the script ends and are not passed to the parent process.

Jon
  • 3,573
  • 2
  • 17
  • 24