Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to escape ${} in a JS template string

I am creating a bash command:

const k = cp.spawn('bash');

k.stdin.end(`
  alias ssh='ssh "${SSH_ARGS[@]}"'
`);

but of course, I have to escape it. I assume the best way to escape it, is using:

 `alias ssh='ssh "\${SSH_ARGS[@]}"'`

can anyone explain why that works?

like image 839
Alexander Mills Avatar asked Jul 09 '19 20:07

Alexander Mills


People also ask

How do I escape a string in JavaScript?

Using the Escape Character ( \ ) We can use the backslash ( \ ) escape character to prevent JavaScript from interpreting a quote as the end of the string. The syntax of \' will always be a single quote, and the syntax of \" will always be a double quote, without any fear of breaking the string.

How do you escape template literal?

To escape a backtick in a template literal, put a backslash ( \ ) before the backtick. Dollar signs can be escaped as well to prevent interpolation.

Do I need to escape in JavaScript string?

TL;DR there's no need for crazy hacks like string concatenation or char literal escapes — just escape it as such: var snaphtml = '<\/script>'; Also, note that this is only necessary for inline scripts.

How to print the escape characters in JavaScript?

Escape characters are characters that can be interpreted in some alternate way then what we intended to. To print these characters as it is, include backslash ‘\’ in front of them. Following are the escape characters in JavaScript − Following is the code implement escape character Backslash in javaScript −

How to escape a quote in a string in JavaScript?

In JavaScript, the escape character for quotes used in strings is the \ (backslash) character. So, to prevent the quotes within the string from interfering with the JavaScript syntax for defining the string, simply place a backslash before them like so:

How to escape a backtick in a template literal?

To escape a backtick in a template literal, put a backslash ( \) before the backtick. Any newline characters inserted in the source are part of the template literal. Using normal strings, you would have to use the following syntax in order to get multi-line strings: Using template literals, you can do the same like this:

How do you write a string in JavaScript?

JavaScript Strings. A JavaScript string is zero or more characters written inside quotes. Example. var x = "John Doe"; Try it Yourself ». You can use single or double quotes: Example. var carName1 = "Volvo XC60"; // Double quotes. var carName2 = 'Volvo XC60'; // Single quotes.


2 Answers

Escaping just the $ works for the same reasons that ordinary curly braces don't throw errors — an expression within a template string is identified by ${ at the beginning and } at the end. If the dollar sign is escaped, it isn't interpreted as part of the ${ keyword, and the curly braces are interpreted as normal characters.

like image 157
IronFlare Avatar answered Oct 19 '22 08:10

IronFlare


Because the backslash \ is the escape character as usual, also in template strings. It prefixes the ${ sequence that would otherwise be interpreted as a delimiter.

like image 20
Bergi Avatar answered Oct 19 '22 08:10

Bergi