Escaping a $ (dollar sign) so it doesn't get treated as a variable

Using grafana v7.5.15 (the docker image), I’m looking for a way to escape a $ so it doesn’t get interpreted as a template variable.

I’ve tried:

  • \$ ← gets passed through as is
  • $$ ← gets passed through as is
  • \\$ ← gets passed through as is

but they don’t do what I want (pass through the string $1 but without grafana attempting to interpolate it).

Here’s an example prometheus query:

label_replace(
  rate(container_cpu_usage_seconds_total{name=~"${cluster}"}[5m]),
  "alloc",
  "$1",
  "name",
  ".*"
)

Right now grafana treats $1 as a variable replacement, but fortunately does nothing since the 1 variable doesn’t exist. But we have more complex use cases and I’d like to try and avoid this in the future.

2 Likes

do you think you could try and replicate this on our public sandbox, https://play.grafana.org? :pray:

I might suggest upgrading to at least Grafana 8, as we aren’t really supporting v7 anymore

2 Likes

granted, the problem might still exist :person_shrugging:

1 Like

Can do; I tried on the template variable dashboard.

source:

## Escape a $?

```
${servers}
$${servers}
\${servers}
\\${servers}
```

rendered:

Escape a $?

test.1, test2
$test.1, test2
\test.1, test2
\\test.1, test2

image

(removing the {} made no difference)

Desired output:

${servers}

Running into the same problem with Grafana 8.3.6. Need to figure out how to escape the capture group reference.

1 Like

Run into the same problem, found a workaround: make a constant nothing with empty value, and then $${nothing}variable is rendered as $variable

1 Like

Ah! that’s a good workaround.

Or, possibly more clearly, add a constant variable dollar=$, then

${dollar}1

becomes →

$1