groovy script - sh with variable groovy script - sh with variable shell shell

groovy script - sh with variable


You have to use double quotes instead of single in order to replace expressions in the string:

def COLORnode('nodename'){    stage ('color') {        COLOR = "green"        echo "color is $COLOR"        sh """COLOR=${COLOR}        echo $COLOR"""    }}

If single quotes need to be used for some reason, try concatenating using +:

sh '''COLOR=''' + COLOR + '''    echo ''' + COLOR


If the code here is meant to assign the groovy variable value ("green") to the environment variable COLOR, and echo $COLOR is meant to print out the shell variable, the $ needs to be escaped like so that the shell can read it, like this:

sh """COLOR=${COLOR}echo \$COLOR"""


COLOR = "green"

sh label: '', script: "echo $COLOR"