简体   繁体   中英

Exporting environment variables from script with whitespaces

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...)

eval "$(aws-env)"

它适用于使用转义空格和引号。

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]="

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM