<p>Either use a single-quoted string, or escape the dollar sign with a backslash:</p>
<pre><code>echo('This is a literal dollar sign: $')
echo(&quot;This is a literal dollar sign: \$&quot;)
</code></pre>
<p>Both will print <code>This is a literal dollar sign: $</code> to the Jenkins console output.</p>
<p>By the way, this question was already <a href="https://stackoverflow.com/q/55454137/1824868">asked and answered over on StackOverflow</a>.</p>

CC BY-SA 4.0
2022-12-12T23:23:47.887Z
2022-12-12T23:23:47.887Z
jayhendren
<p>I'm making a declarative Jenkins pipeline which has a for loop to iterate over the output of a bash command. In that command it is necessary to use bash arguments e.g. &quot;$1&quot; for first argument. And it is a series of pipes with many types of quotes.</p>
<p>I tried using the triple double quotes for a raw string in groovy but it seems groovy is still trying to parse &quot;$1&quot; as me trying to reference the value of a variable named &quot;1&quot;. So, this is not expected &quot;raw string&quot; behavior.</p>
<p>How does one go about doing this?</p>
<p>Here's an example of what the command is like.</p>
<pre class="lang-bash prettyprint-override"><code>kubectl get namespaces --no-headers -o=custom-columns=&quot;:.metadata.name,:.metadata.creationTimestamp&quot; | grep -E &quot;myproject_&quot; | awk '$2 &lt;= &quot;'$(date -d'now-2 days' -Ins --utc | sed 's/+0000/Z/')'&quot;{print $1}' | sed 's/myproject_//g'&quot;&quot;&quot;
</code></pre>
<p>Ignore the specifics of the command. My question is purely how does one get around the dollar sign syntax errors. The actual command is something different.</p>

<jenkins><jenkins-pipeline><groovy>
How to escape dollarsign in groovy shell command?
kotezbuzzi
# How to escape dollarsign in groovy shell command?

(Asked by: username_0 on 2022-12-11)

I'm making a declarative Jenkins pipeline which has a for loop to iterate over the output of a bash command. In that command it is necessary to use bash arguments e.g. "$1" for first argument. And it is a series of pipes with many types of quotes.
I tried using the triple double quotes for a raw string in groovy but it seems groovy is still trying to parse "$1" as me trying to reference the value of a variable named "1". So, this is not expected "raw string" behavior.
How does one go about doing this?
Here's an example of what the command is like.
kubectl get namespaces --no-headers -o=custom-columns=":.metadata.name,:.metadata.creationTimestamp" | grep -E "myproject_" | awk '$2 <= "'$(date -d'now-2 days' -Ins --utc | sed 's/+0000/Z/')'"{print $1}' | sed 's/myproject_//g'"""

Ignore the specifics of the command. My question is purely how does one get around the dollar sign syntax errors. The actual command is something different.

**Tags:** jenkins, jenkins-pipeline, groovy


---

## 1 answers

### Answer by username_1 on 2022-12-12. Score: 1

Either use a single-quoted string, or escape the dollar sign with a backslash:
echo('This is a literal dollar sign: $')
echo("This is a literal dollar sign: \$")

Both will print This is a literal dollar sign: $ to the Jenkins console output.
By the way, this question was already asked and answered over on StackOverflow.


---

