How to set variables in a multi-line shell script

2019-01-23 00:45发布

Suppose I have a Groovy script in Jenkins that contains a multi-line shell script. How can I set and use a variable within that script? The normal way produces an error:

sh """
    foo='bar'
    echo $foo
"""

Caught: groovy.lang.MissingPropertyException: No such property: foo for class: groovy.lang.Binding

2条回答
Melony?
2楼-- · 2019-01-23 01:28

You need to change to triple single quotes ''' or escape the dollar \$

Then you'll skip the groovy templating which is what's giving you this issue

查看更多
家丑人穷心不美
3楼-- · 2019-01-23 01:40

I'm just putting a '\' on the end of line

sh script: """\
  foo='bar' \
  echo $foo \
""", returnStdout: true

This statement works on my script.

查看更多
登录 后发表回答