rhaiscript / rhai

Rhai - An embedded scripting language for Rust.
https://crates.io/crates/rhai
Apache License 2.0
3.82k stars 180 forks source link

Escape string interpolation #694

Open hack3ric opened 1 year ago

hack3ric commented 1 year ago

Writing Bash scripts in Rhai is somewhat uncomfortable when it comes to Bash's ${VAR} syntax:

cmake .. ${cmake_args[@]}
ninja

Currently I have to write

let script = `
  cmake .. ${"${cmake_args[@]}"}
  ninja
`;

Could there be some forms of escaping this syntax, either double dollar sign ($$) similar to writing back-ticks (``), or a more general escape pattern that includes \$ (or more broadly, \`)?

schungx commented 1 year ago

The thing is, anything wrapped in backticks are supposed to be literal, meaning that there are no escapes.

True that it does make embedding ${ ... } inside back-ticks difficult...

You can use double quotes if you don't need interpolation:

let script = "cmake ${cmake_args[@]}\n\
              ninja";

But, of course, other than not having interpolation, you have to live with the ugly \n\ at the end of each line...