variable expansion and the 2 types of quotes in workflow script

436 views
Skip to first unread message

Michael Neale

unread,
Nov 30, 2015, 5:06:21 PM11/30/15
to Jenkins Users
I would like some clarity on the different types of quotes in workflow script. Take this contrived example: 

env.FOO = 42
def FOO2 = 43

node {
  sh 'echo $FOO'
  sh "echo $FOO2"
}

This works - as you would expect. However, if you switch the quotes around, it fails. It seems single quote means that jenkins won't expand it and instead it will be passed directly to the shell (whence the environment variable is available). In the second shell script, the double quotes mean that Jenkins expands it based on variables in scope. If you make it single quote, there is no variable in the shell environment. 

Is this deliberate behavior, and have I understood it correctly? 

Slide

unread,
Nov 30, 2015, 5:09:55 PM11/30/15
to Jenkins Users
This is the behavior of the groovy language. Variables are interpolated in double quoted strings, but not in single quoted strings.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/87d96032-8670-4fc2-b81c-bc57bb8f6527%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Neale

unread,
Nov 30, 2015, 5:34:16 PM11/30/15
to Jenkins Users
Ah yes of course, thanks. Same as ruby. 

Michael Neale

unread,
Nov 30, 2015, 5:35:38 PM11/30/15
to Jenkins Users
Although in Ruby the $VARIABLE wouldn't collide with ruby interpolation and be left alone. It seems in groovy that whilst ${variableName} is idiomatic, it isn't enforced (at least I assume that is what is happening, it is failing to expand). Somewhat annoying of groovy. 
Reply all
Reply to author
Forward
0 new messages