Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jenkins pipeline script - How to use variable as a variable name

Jason Davis November 1, 2022

Hi

I have a groovy script as my Jenkins pipeline script, which looks something like

node("nodeName") {

. . . (some groovy code)

      sh """

    ......

     < This section of the shell script calculates the value of the variable varName, example being BUILD_NUMBER >

     ......

    echo "${env.BUILD_ID}"    # This prints expected output. Example : 51

      export varName="env.BUILD_NUMBER"

       echo "${"\$varName"}"   # This  prints the output as env.BUILD_NUMBER and not 51

"""

...

}

In the above example, I had hard coded value of varName as env.BUILD_NUMBER. In my actual script, value of varName will be calculated during execution.

 

In bash script, this result can be achieved as follows:

 

#!/bin/bash
variable_1="abc"

variable_2=variable_1

echo "${!variable_2}"

 

Exclamation mark (!) doesn't work in shell script  inside Jenkins pipeline

 

Can someone please explain, how to address my requirement ?

 

Thank you

Jason

 

1 answer

1 accepted

0 votes
Answer accepted
Jason Davis November 1, 2022

I am sorry that by mistake, have posted my query here. Please ignore.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events