我有一堆管道 shell 命令,可以提供一些 env 输出。我想将它们设置为环境变量,以便将附加命令添加到链中
Kevins-MBP:ops kevzettler$ eb printenv | tail -n +2 | sed "s/ //g"
NODE_ENV=staging
RDSPassword=changme
RDSHost=sa1c7quehy7pes5.lolol.us-east-1.rds.amazonaws.com
RDSUsername=derp
答案1
您可能想要:
source <(eb printenv | tail -n +2 | sed 's/ //g; s/^/export /')
your_next_command_that_uses_those_env_vars
一个测试:
定义一个打印出示例变量定义的函数
function eb { echo " NODE_ENV=staging RDSPassword=changme RDSHost=sa1c7quehy7pes5.lolol.us-east-1.rds.amazonaws.com RDSUsername=derp" }
调用它来查看管道产生什么
$ eb printenv | tail -n +2 | sed 's/ //g; s/^/export /' export NODE_ENV=staging export RDSPassword=changme export RDSHost=sa1c7quehy7pes5.lolol.us-east-1.rds.amazonaws.com export RDSUsername=derp
获取该输出,测试当前 shell 和新 shell 以查看它是否已导出
$ source <(eb printenv | tail -n +2 | sed 's/ //g; s/^/export /') $ echo $NODE_ENV staging $ sh -c 'echo $NODE_ENV' staging