Scala: Unable to set environment variable

百般思念 提交于 2020-01-21 15:19:38

问题


Friends,

I'm trying to set the environment variable "asdf" in my Scala shell, as described here

These are my commands:

scala> import scala.sys.process.Process
import scala.sys.process.Process

scala> Process(Seq("bash", "-c", "echo $asdf"), None, "asdf" -> "Hello, world!").!
Hello, world!
res18: Int = 0

But when i try to read the environment variable back:

scala> sys.env.get("asdf")
res19: Option[String] = None

The output says "None". How do i properly set my environment variable in the current session?

PS - Please do not downvote this; i'm trying really hard but unable to get past my issue


回答1:


It is not permitted for a Scala/Java process to modify its own environment. You can use the scala.util.Properties object to inspect environmental variables and properties. (Docs are here.) The properties can be added/removed/changed but the environmentals cannot.




回答2:


It has nothing to do with Scala, you are just misunderstanding the situation. The map at the end of the line

scala> Process(Seq("bash", "-c", "echo $asdf"), None, "asdf" -> "Hello, world!").!

doesn't change the environment of of this process, the one you are typing into; it changes the environment of the child process that the Process() function creates.



来源:https://stackoverflow.com/questions/42639105/scala-unable-to-set-environment-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!