The aim of this page is to explain string interpolation based on the particular example of using JQ tool and JavaScript.
- String interpolation allows inserting variables and expressions into strings.
- JQ tool uses
\(.variable)
for string interpolation. - JavaScript uses
${variable}
for string interpolation. - Example of JQ tool:
echo '{ "id": 12345, "subject": "Subject line here" }' | jq -r '"\(.id) \(.subject)"'
- In JQ tool, variables are enclosed in parentheses and prefixed with a backslash.
- Example of JavaScript:
const id = 12345;
const subject = "Subject line here";
const result = `${id} ${subject}`;
console.log(result); // Output: 12345 Subject line here
- In JavaScript, template literals are enclosed in backticks.
- Variables are embedded using `${}`.
- Template literals allow embedding expressions and multiline strings.
- String interpolation is useful for generating dynamic strings.