简体   繁体   中英

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM