Search code examples
scalaprocessprocessbuilder

Scala: Read variables defined in shell script


From within my Scala application, I want to execute a script (e.g. setenv.sh) which sets some variables. I then want to read back these variables in my application to do some further processing. Here is my sample setenv.sh file

#!/bin/bash

TEST=0000

I have the following but it doesn't work. I tried many alternatives but failed to do it.

Seq("bash", "-c", "source setenv.sh && env").!!

The above command still gives me the old environment variables and i cannot see TEST.

My goal is to just load all the variables in setenv.sh file and use them in my application.


Solution

  • Can't work that easy. Keep in mind that you are opening a new shell process. That process sees the setting - to then end.

    If at all try explicitly exporting that value.

    Alternatively you could print the values to stdout and have Scala parse that output.