Julia command line evaluation with string in the expression

I am using PowerShell on Windows. Did not figure out how I can include a string in the expression when evaluating it in the command line.
Examples:

PS F:\WJ\SmartPark\CMS\code\COPT_HTTP> julia  -e 'x = 123'
PS F:\WJ\SmartPark\CMS\code\COPT_HTTP> julia  -e "x = 'ok'"
ERROR: syntax: character literal contains multiple characters
Stacktrace:
 [1] top-level scope
   @ none:1
PS F:\WJ\SmartPark\CMS\code\COPT_HTTP> julia  -e 'x = "ok"'
ERROR: UndefVarError: ok not defined
Stacktrace:
 [1] top-level scope
   @ none:1
PS F:\WJ\SmartPark\CMS\code\COPT_HTTP> julia  -e "x = "ok""
ERROR: syntax: incomplete: premature end of input
Stacktrace:
 [1] top-level scope
   @ none:1
PS F:\WJ\SmartPark\CMS\code\COPT_HTTP> julia  -e "x = \"ok\""
ERROR: syntax: incomplete: invalid string syntax
Stacktrace:
 [1] top-level scope
   @ none:1
PS F:\WJ\SmartPark\CMS\code\COPT_HTTP> julia  -e " x = \"ok\" "
ERROR: syntax: incomplete: invalid string syntax
Stacktrace:
 [1] top-level scope
1 Like

Works for me in a Unix shell, where both julia -e 'x = "ok"' and julia -e "x = \"ok\"" work fine. So it must be some weirdness with PowerShell quoting rules or Windows command-line parsing. You could try WSL as a workaround.

1 Like

Yes, it seems so. I found that julia -e "x = \"ok\"" works in Windows CMD terminal, but still failed in PowerShell.

1 Like

Maybe someone who knows PowerShell’s quoting rules better can chime in, but I managed to get this to work on PowerShell for Linux.

PS /home/anon> julia -e 'x = \"ok\"; println(x)'
ok

In most other shells, those backslashes wouldn’t be necessary, but something weird is going on.

2 Likes

In Powershell, you do not need to escape double quotes within single quotes. To escape double quotes, use two of them.

How about installing MobaXterm and getting a bash shell?
This may not be what you want, but could help.

Could you please give an example?

@Shuhua
Double double quotes.

PS /home/anon> julia -e 'x = ""ok""; println(x)'
ok

Surprisingly, this doesn’t work:

PS /home/anon> julia -e "x = ""ok""; println(x)"
ERROR: UndefVarError: `ok` not defined
Stacktrace:
 [1] top-level scope
   @ none:1

However, this works.

PS /home/anon> julia -e "x = \""ok\""; println(x)"
ok

Whenever I get to quoting-hell in the Windows command line, I break out the sh that came with git. For me, that is most common when I need to use curl.

1 Like