Reputation:
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
Reputation: 12061
You can alternatively indeed use the ENV
variable. Environment variables are not available to subprocesses unless they are export
ed. So a revision of your code,
export y=10
echo $y
julia -e 'println(ENV["y"])'
would work fine.
Upvotes: 3
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:
https://www.gnu.org/software/bash/manual/html_node/Quoting.html#Quoting
Which characters need to be escaped in Bash? How do we know it?
Upvotes: 4