google / zx

A tool for writing better scripts

Home Page:https://google.github.io/zx/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

interpolation example not in docs?

Morriz opened this issue · comments

Expected Behavior

...
const cmd = 'ls -als /tmp' # predefined in other context
const res = await $${cmd}

Actual Behavior

/usr/bin/bash: ls -als /tmp: command not found

I did find that if I split the cmd by space it works:

  const res = await $`${cmd.split(' ')}`

I lost some time finding this out. I will try to find some time for a doc PR.

Can we re-open this? I get errors here:

const cmd = 'pwd && ls -als .'

which gives this error:

/usr/local/bin/bash: pwd && ls -als .: command not found

because, well, reasons...so we try the next:

...
const res = await $`${cmd.split(' ')}`

which translates to pwd '&&' ls -als ., so that is also not possible.

How on earth can we create commands up front so that zx doesn't escape special constructs like && or || etc etc etc?

On earth:

await $`pwd && ls -als .`

And read https://github.com/google/zx/blob/main/docs/quotes.md carefully one more time.

But you are not paying attention to the fact that the command is created and stored in a variable. You are most of the time too quick to conclude. And rude

so please re-open this as it is still valid

It is not valid. Zx, by design, escapes everything that is passed in ${ }. For security reasons.

If you need to construct a command entirely you should use child_process directly, as zx does not provide such feature (this is intentional).

Of course, there is plenty of work arounds, if you must use zx for this. Split the cmd string split(' ') pass cmd as param to shell await $`/bin/sh -c ${cmd}`

Intentional? Not configurable because you oppose it. How is this usable when we need to be able to provide commands via config into code? This is becoming more like a joke

I am sorry for not rooting deep enough, but I found out later that we can just remove the quoting functionality altogether, which works wonders! Why did you not mention it once?

    const q = $.quote
    $.quote = (v) => v

All our problems are solved. If people ask for it I recommend you give them what they need. Maybe I did not search for it hard enough in the docs, so I will check once again.

Nope, not given any attention in the docs. That would have saved us all a lot of friction.