user2329125
user2329125

Reputation:

eval expression with environment variables

Version 0.6

I want to use julias -e(val) option with environment variables. How can I do that?

Example:

y=10
echo $y
julia -e 'println($y)'

the echo works, as expected. But the julia line does not work. ERROR: unsupported or misplaced expression $. Now how do I make this work?

I tried it with ENV["y"] but it does not find the variable.

Upvotes: 2

Views: 2330

Answers (2)

Fengyang Wang
Fengyang Wang

Reputation: 12061

You can alternatively indeed use the ENV variable. Environment variables are not available to subprocesses unless they are exported. So a revision of your code,

export y=10
echo $y
julia -e 'println(ENV["y"])'

would work fine.

Upvotes: 3

Dan Getz
Dan Getz

Reputation: 18227

The question is not really Julia related, but more shell related. The shell does not replace environment variables in strings surrounded by ' (single quote), but does replace them in double quoted strings (surrounded by "). So the solution would be to do:

julia -e "println($y)"

The issues become more complicated if you want to use the $ sign in the Julia expression or " itself - for these there are documented escaping rules. See, for example:

Upvotes: 4

Related Questions