Quoting when constructing commands problematic?

I think the construction of commands objects on Windows uses improper quoting for directories. See PlotlyJS causes errors, can't figure out how to use PlotlyLight - how to use Plotly from Julia? - #27 by PetrKryslUCSD

It’s really not clear what you think is broken. Can you elaborate? MWE?

image
The command to change the folder doesn’t work, even though the folder it does exist, because the quote needs to be cd "hey and", not cd 'hey and'.

Apostrophe quoting in cmd is a problem:


Note that running commands in cmd seems to be the default for Julias on Windows systems.
Apostrophe quoting in Powershell works fine:

Actually that’s nothing to do with quoting, and simply because cd is not actually an executable. Here I get the same error with cd temp even though temp does exist.

julia> run(`cd`)
ERROR: IOError: could not spawn `cd`: no such file or directory (ENOENT)

julia> run(`cd temp`)
ERROR: IOError: could not spawn `cd temp`: no such file or directory (ENOENT)

cd is actually a builtin function to most shells, so can only be executed from a shell (such as bash or powershell), and Julia is not a shell.

2 Likes

No, using run does not launch any shell, cmd or otherwise. It executes the given command directly. For example run(`cd 'foo bar'`) finds an executable called cd and executes it with the single argument foo bar.

The quoting you use when entering a command in Julia is intentionally similar to how you quote things in shells, but no shell is actually ever used. The quoting is forgotten and the command is internally represented as a vector of strings such as ["cd", "foo bar"] because this is all you need if you don’t use a shell.

2 Likes

For Julia on Windows this works:

cmd = """cd "hey and"; ls > test.txt"""
run(`powershell -command $cmd`)
1 Like