Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia run external programm with environmental variables: Don't interpolate $

Tags:

julia

I'm trying to run a shell command from Julia which needs to have an environment variable set to some specific value. I have two problems:

  1. How to set environment variables to be used by Julia's
    run(command, args...; wait::Bool = true) command?

  2. How to pass special sign $ to this process without interpolating it? I want to test if the variable is available for my program.

What I have done so far:

Let's say I want to define an environment variable FOO=bar and check if it's accessible within the shell with shell command echo $FOO.

To prevent Julia interpolating $ I already quoted it like explained in the official documentation but then echo is printing $PATH and not its value.

So for FOO I got the following output

julia> run(`echo '$FOO'`)
$FOO
Process(`echo '$FOO'`, ProcessExited(0))

but would have expected something like

julia> run(`echo '$FOO'`)

Process(`echo '$FOO'`, ProcessExited(0))

if FOO is undefined or

julia> run(`echo '$FOO'`)
bar
Process(`echo '$FOO'`, ProcessExited(0))

if the value is set to bar.

like image 253
AnHeuermann Avatar asked Aug 07 '19 12:08

AnHeuermann


1 Answers

Check out the Julia documentation on environment variables. You can set an environment variable with:

julia> ENV["FOO"] = "bar"
"bar"

and you can retrieve the value of an environment variable with:

julia> ENV["FOO"]
"bar"
julia> ENV["PATH"]
"really long string of my path"

As you've already stated, you can avoid interpreting the $ by single-quoting that part of your run command. I'm not totally sure what you are looking for there.

like image 124
Engineero Avatar answered Mar 02 '23 00:03

Engineero