0

The following code is a very simplified version of my code just for a better explanation:

def String FOLDER_NAME = "TestFolder"

def createFolder() {
    sh('''
        ls
        mkdir ${FOLDER_NAME}
        ls
    ''')
}

I want to use the name, stored inside the FOLDER_NAME variable, to create a new folder. The problem is that the current code does not use it. My first approach was to use ' ' around the variable access like this:

def createFolder() {
    sh('''
        ls
        mkdir '${FOLDER_NAME}'
        ls
    ''')
}

But this creates a new folder called ${FOLDER_NAME} and does not use the variable value.

My question is, how do I have to change my code so that it uses the variable value, not the variable call?

ErikWe
  • 191
  • 3
  • 18
  • Possible duplicate of [What's the difference of strings within single or double quotes in groovy?](https://stackoverflow.com/questions/6761498/whats-the-difference-of-strings-within-single-or-double-quotes-in-groovy) – mkobit Mar 16 '18 at 16:06

1 Answers1

3

Your solution will be resolved by differentiating between groovy's single and double quotes.

String replacements are done inside double quotes "" only. Therefore, to resolve the issue just change sh command to:

def createFolder() {
    sh("""
        ls
        mkdir '${FOLDER_NAME}'
        ls
    """)
}
yamenk
  • 46,736
  • 10
  • 93
  • 87