HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to escape dollarsign in groovy shell command?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
shelldollarsignhowcommandgroovyescape

Problem

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

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.

Solution

Either use a single-quoted string, or escape the dollar sign with a backslash:

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.) 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.

Code Snippets

echo('This is a literal dollar sign: $')
echo("This is a literal dollar sign: \$")

Context

StackExchange DevOps Q#17055, answer score: 1

Revisions (0)

No revisions yet.