简体   繁体   English

如何从 Heroku 为单个 bash 命令设置环境变量,而不是为整个 shell Z21D9F4440CFB0E51508A 设置环境变量?

[英]How can I set environment variables from Heroku for a single bash command, not for the whole shell session?

I can set an environment variable that is only available to that specific command, and doesn't remain available in that shell session afterwards.我可以设置一个仅对该特定命令可用的环境变量,之后在该 shell session 中不可用。 (I'm sure my terminology is not accurate here – please correct me.) (我确定我的术语在这里不准确——请纠正我。)

$ FOO=hello ruby -e 'puts ENV["FOO"]'
hello
$ echo $FOO
(no output)

I can also get environment variables from Heroku:我还可以从 Heroku 获取环境变量:

$ heroku config:get --shell FOO BAR
FOO=hello
BAR=goodbye

Now, how can I combine these techniques – getting environment variables from Heroku and making them available only to a single command run?现在,我如何结合这些技术——从 Heroku 获取环境变量并使其仅可用于单个命令运行?

This works but $FOO remains available, which is not what I want:这可行,但 $FOO 仍然可用,这不是我想要的:

export `heroku config:get --shell FOO BAR`
echo $FOO

So what I want to achieve is this:所以我想要实现的是:

$ some_shell_magic(`heroku config:get …`) ruby -e 'puts ENV["FOO"]'
hello

Any ideas?有任何想法吗?

The underlying use case is to be able to run certain commands from a developer machine, using production config values, but not leaving those values around because they could accidentally end up being used by some other command later.底层用例是能够从开发人员机器上运行某些命令,使用生产配置值,但不要留下这些值,因为它们可能会意外地最终被其他命令使用。

Based on what you gave as heroku command, this should achieve what you wanted:根据您提供的 heroku 命令,这应该可以实现您想要的:

env $(heroku config:get …) ruby -e 'puts ENV["FOO"]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 bash 的其他命令的 output 设置多个环境变量? - How to set multiple environment variables from output of other command in bash? 评估文件以为单个命令设置环境变量时如何支持注释? - How can I support comments when eval'ing a file to set environment variables for a single command? 如何从脚本中设置bash环境变量? - How do I set bash environment variables from a script? 如何防止patch命令退出bash shell? - How can I prevent the patch command from exiting a bash shell? 如何将环境变量从 bash shell (PostgreSQL) 传递给 sql 文件 - How to pass environment variables to a sql file from bash shell (PostgreSQL) 如何将 shell 中的所有环境变量传递给它调用的 bash 脚本? - How do I pass all the environment variables from my shell to the bash script it calls? 如何获取STDIN并在bash shell上使用它来设置环境变量? - How can I take STDIN and use it on the bash shell to set an environment variable? 如何在Mac上的Bash Shell中设置环境变量完成 - How do I set environment variable completion in a bash shell on mac 如何将环境变量从Shell脚本导入当前的Ruby环境? - How can I import environment variables from a shell script into the current Ruby environment? 我可以在Bash的命令行上设置环境变量吗? - Can I set an environment variable on Bash's command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM