Search code examples
jenkins-pipelineshgroovyshell

sh how to Append variable's value to file into sh double quoted multiline


I need to append a string into a file. The string is made up of some prefix + variable. This is in Jenkins pipeline using Groovy Syntax, so the only way to read variable's value is with multiline double quoted sh.

sh """
  ...some lines of code affecting the same scope...

  echo "sdk.dir=${ANDROID_HOME}" > local.properties

  ...some lines of code affecting the same scope...
  """

Actual:

WorkflowScript: 101: illegal string body character after dollar sign;

   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 101, column 49.

     echo "sdk.dir=${ANDROID_HOME}" > local

Solution

  • Did you try to escape the $ sign with a slash as the error mentioned?

    Also you’re using the same double quote, try to replace the “sh” command to a triple single quote and use the double quote for the shell redirect as you did now.

    Or last option you can escape the whole expression:

    \\” express \\”