14

I want to run my process from scala, with several environment variables modified. For example:

Seq("bash","echo $asdf") !

and $asdf set to some value. Is there a way to do this from scala?

EDIT:

The closest I got to it so far:

val pb = new java.lang.ProcessBuilder("bash","echo $asdf")
pb.environment.put("asdf","value") }
val p = pb.start()
io.Source.fromInputStream(p.getInputStream).getLines.toList.foreach(println)
p.waitFor()

But this is ugly.

Rogach
  • 26,050
  • 21
  • 93
  • 172

1 Answers1

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

See Process.

Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • 1
    For the first time ever, my answer is longer than the Sobral answer it duplicates. Ironically, this answer more precisely answers what the duplicate question was probably trying to ask. http://stackoverflow.com/a/18411947/1296806 – som-snytt Aug 24 '13 at 05:00
  • @som-snytt This was a rather poor answer of mine. Shame on me! :) – Daniel C. Sobral Aug 24 '13 at 06:08
  • I tried this and it doesn't work, it tries to interpret "echo $asdf" as an executable -- gives `bash: echo $asdf: No such file or directory`. – Elias Dorneles Sep 19 '13 at 19:37
  • 1
    I got it working adding a -c: `Process(Seq("bash", "-c", "echo $asdf"), None, "asdf" -> "Hello, world!").!` – Elias Dorneles Sep 19 '13 at 19:39