2

I'm able to run the following shell script but couldn't run from Jenkins pipeline code.

Try 1.

node('buildnode') {

def value = "Myvalue"

def key = "Mykey"

sh '''

DATA=$(printf "%-50s \"$key\"" "$value")

echo "$DATA"

'''

}

output:

++ printf '%-50s ' ''
+ DATA=' 

Try 2:

Tried with sh " " "

DATA=$(printf "%-50s \"$key\"" "$value")

echo "$DATA"

" " "

output: :

illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}"

Can someone help me?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
R.V
  • 199
  • 1
  • 3
  • 13

1 Answers1

5

This should work.

node('buildnode') {
    def value = "Myvalue" 
    def key = "Mykey"

    sh """
    DATA=\$(printf "%-50s \"${key}\" \"${value}\"")
    echo "\$DATA"
    """
}

You also need to escape $ when calling new subshell under """ """

DATA=$(printf "%-50s \"${key}\" \"${value}\"")
Tin Topolovec
  • 106
  • 1
  • 7
  • Thanks. It actually worked. How Can I get double quotes for "value". I have tried- DATA=\$(printf "%-50s \"${key}\" "\"${value}\""") but no luck. – R.V May 16 '18 at 05:53