Hello David,
We have \gset to set some parameters, but not ones in the environment, so I fixed this with a new analogous command, \gsetenv. I considered refactoring SetVariable to include environment variables, but for a first cut, I just made a separate function and an extra if.
My 0.02€: ISTM that you do not really need that, it can already be achieved with gset, so I would not bother to add a gsetenv.
sh> psql SELECT 'Calvin' AS foo \gset \setenv FOO :foo \! echo $FOO Calvin -- Fabien.