3

Big picture of the issue: I'm creating Golang script (http://github.com/Droplr/aws-env) that will be used to secure retrieve environment variables and export them to be used for another process.

As we know, it's not easy doable as script can't export vars outside [1], [2].

So the way we were trying to do it, is basically outputing export statements and then using bash backticks to run that command.

Final usage is like:

`aws-env` && printenv

where printenv should show variables exported by evaluated aws-env output (which contains "export" statements).

The problem arises with variables that contain spaces/new lines etc.

Simplifying underline code, this one works:

$ export A=b\ b
$ printenv | grep A=
A=b b

And this - not:

$ `echo export A=b\ b`
$ printenv | grep A=
A=b

I've went over other Stackoverflow discussions ([3]) but couldn't find clear answer for that problem (mostly the answer is to not use backticks, but with our overall problem we try to solve it wont be so easy...)

Antoni Orfin
  • 301
  • 3
  • 7
  • In the same shell. The usage of the script is like: \`aws-env\` && printenv (printenv should show env variables exported by aws-env). aws-env is just outputing "export ..." that then should be evaluated by backticks. – Antoni Orfin Jul 14 '17 at 08:55
  • Just to confirm your requirement again? what does `aws-env` expected to do? it is not clear – Inian Jul 14 '17 at 08:56
  • Shouldn't this: https://github.com/Droplr/aws-env/blob/master/aws-env.go#L60 be `fmt.Printf("export %s='%s' \n", env, value)`? Note, that in this case if your export statements contain other shell variables, they won't be extrapolated. – favoretti Jul 14 '17 at 09:04
  • @favoretti unfortunately ' also does not work. `$ \`echo "export A='b b'"\` && env | grep A= A=b` – Antoni Orfin Jul 14 '17 at 09:14

2 Answers2

0

eval "$(aws-env)"

It works both with using escaped spaces and with quotes.

AgileZebra
  • 593
  • 6
  • 14
-1

If I follow this correctly, aws-env is going to output something like this:

export A=b\ b
export B=test
export C=this\ is\ a\ test

And you want those commands executed in your current shell? If so, this should work:

. <(aws-env)
printenv | egrep "^[ABC]="
Wayne Vosberg
  • 1,023
  • 5
  • 7
  • This doesn't work. If you create a simple version of his aws-env that just echos a couple of export lines, you'll find that command has no effect. I found out when trying to create a similar Python version of was-env to do the same thing and got a BrokenPipeError using he above. I then tried with a script and it seems that `.` and `source` do not work as expected with process substitution. – AgileZebra Jan 23 '20 at 12:25
  • Isn't this what he is trying to do? `$ ./aws-env export A=b\ b export B=test export C=this\ is\ a\ test $ printenv | egrep "^[ABC]=" $ . <(./aws-env) $ printenv | egrep "^[ABC]=" C=this is a test B=test A=b b ` – Wayne Vosberg Jan 23 '20 at 13:58