lal123
lal123

Reputation: 21

Groovy sh command is not including quotes in my echo command

I am trying to write hostname within quotes to a file. And I am doing this in a Groovy script. Here is my sh Groovy script.

sh "echo Add \"my-hostname\" \"$HOSTNAME\" >> /etc/test.conf"

My expected outcome in /etc/test.conf should be:

Add "my-hostname" "linux-base"

But when I execute my Groovy script the outcome is:

Add my-hostname linux-base

Somebody please help me in sh command script.

Upvotes: 2

Views: 1340

Answers (1)

terdon
terdon

Reputation: 3370

Two workarounds:

  1. Use single quotes:

    sh 'echo Add \"my-hostname\" \"'"$HOSTNAME"'\" >> aa'
    
  2. Use double escaping:

    sh "echo Add \\\"my-hostname\\\" \\\"$HOSTNAME\\\" >> aa"
    

I am not familiar with groovy, are you sure your command doesn't need sh -c?

Upvotes: 1

Related Questions