有没有办法从Linux命令行返回环境变量RAILS_ENV的值?

时间:2022-06-28 11:20:17

In Linux, you can find the value of the $PATH environment variable with the command echo $PATH. In Rails, RAILS_ENV (and the similar RACK_ENV) is an environment variable with values such as "development" and "production". But on the command line, echo RAILS_ENV returns the unhelpful string RAILS_ENV (as you'd expect) and echo $RAILS_ENV returns nothing (presumably because that's not what the environment variable is called). I've also tried env RAILS_ENV and variants. Am I getting the syntax wrong, or what?

在Linux中,您可以使用命令echo $ PATH找到$ PATH环境变量的值。在Rails中,RAILS_ENV(和类似的RACK_ENV)是一个环境变量,其值如“development”和“production”。但是在命令行中,echo RAILS_ENV返回无用的字符串RAILS_ENV(正如您所期望的那样)并且echo $ RAILS_ENV不返回任何内容(可能是因为这不是调用环境变量的内容)。我也试过了env RAILS_ENV和变种。我的语法错了,或者是什么?

Am I right to think that basically the environment variable just doesn't exist until Rails is run? Well...I tried running a Rails server in one terminal, then opened another and then repeated the above commands; no dice.

我是否认为基本上环境变量在Rails运行之前不存在?嗯...我尝试在一个终端中运行Rails服务器,然后打开另一个终端,然后重复上述命令;没有骰子。

Or is the problem that Linux, or my shell, doesn't track this environment variable? But...wouldn't it have to, being an environment variable? I tried looking for it in the output of the env command, and it's not there.

或者Linux或我的shell不跟踪此环境变量的问题是什么?但是......作为一个环境变量,它不是必须的吗?我尝试在env命令的输出中查找它,它不在那里。

Is it possible that the only way to discover the value of the RAILS_ENV environmental variable is through Rails itself, e.g., by calling Rails.env?

是否有可能发现RAILS_ENV环境变量值的唯一方法是通过Rails本身,例如通过调用Rails.env?

UPDATE: this helpful discussion gives the command rails r "puts Rails.env", but I was looking for a command that didn't call Rails itself.

更新:这个有用的讨论给出了命令rails r“puts Rails.env”,但我正在寻找一个不调用Rails本身的命令。

1 个解决方案

#1


1  

You were right with echo $RAILS_ENV. It is likely not set, so it is defaulting to development. This is done within the Rails codebase.

你使用echo $ RAILS_ENV是对的。它可能没有设置,因此它是开发的默认值。这是在Rails代码库中完成的。

$ export RAILS_ENV=development
$ echo $RAILS_ENV #=> development

In another console

在另一个控制台

# In a new window
$ echo $RAILS_ENV
$ rails runner "puts Rails.env" #=> development
$ RAILS_ENV=test rails runner "puts Rails.env" #=> test

No RAILS_ENV variable is set, to rails defaults to development

没有设置RAILS_ENV变量,rails默认为开发

Here is the equivilent in Bash.

这是Bash中的等值。

if [ -z "$RAILS_ENV" ]; then
  export RAILS_ENV="development";
fi
echo $RAILS_ENV

#1


1  

You were right with echo $RAILS_ENV. It is likely not set, so it is defaulting to development. This is done within the Rails codebase.

你使用echo $ RAILS_ENV是对的。它可能没有设置,因此它是开发的默认值。这是在Rails代码库中完成的。

$ export RAILS_ENV=development
$ echo $RAILS_ENV #=> development

In another console

在另一个控制台

# In a new window
$ echo $RAILS_ENV
$ rails runner "puts Rails.env" #=> development
$ RAILS_ENV=test rails runner "puts Rails.env" #=> test

No RAILS_ENV variable is set, to rails defaults to development

没有设置RAILS_ENV变量,rails默认为开发

Here is the equivilent in Bash.

这是Bash中的等值。

if [ -z "$RAILS_ENV" ]; then
  export RAILS_ENV="development";
fi
echo $RAILS_ENV