2

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.

Dan Getz
  • 17,002
  • 2
  • 23
  • 41

2 Answers2

4

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:

Dan Getz
  • 17,002
  • 2
  • 23
  • 41
3

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.

Fengyang Wang
  • 11,901
  • 2
  • 38
  • 67