MoreFamed
MoreFamed

Reputation: 111

Sending psql variable into shell

I'd like to send variable in psql into shell to do some tricky things with it there and use the result in psql again. See:

\set ENVIR `echo :HOST | cut -f2 -d-`
\echo :ENVIR

However, the psql variable seems to be not interpolating in backticks as I would expect: shell's echo :HOST gives just :HOST. Help, please.

Upvotes: 0

Views: 277

Answers (1)

Dmitri
Dmitri

Reputation: 9157

You need to use \setenv to export the psql variable to the shell:

\set HOST 'the-host-name'
\setenv HOST :HOST
\set ENVIR `echo $HOST | cut -f2 -d-`
\echo :ENVIR

produces:

host

Edit

Of course, your example can be done with postgres string functions, with no need for shelling out.

Upvotes: 1

Related Questions