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

**URL:** <https://community.jenkins.io/t/jenkins-pipeline-script-how-to-use-variable-as-a-variable-name/4385>\
**Category:** Using Jenkins\
**Created:** [November 1, 2022, 10:25pm UTC](https://community.jenkins.io/t/jenkins-pipeline-script-how-to-use-variable-as-a-variable-name/4385 "2022-11-01T22:25:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JasonDavis](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@JasonDavis](https://community.jenkins.io/u/JasonDavis)\
**Post date:** [November 1, 2022, 10:25pm UTC](https://community.jenkins.io/t/jenkins-pipeline-script-how-to-use-variable-as-a-variable-name/4385/1 "2022-11-01T22:25:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [November 2, 2022, 12:48am UTC](https://community.jenkins.io/t/jenkins-pipeline-script-how-to-use-variable-as-a-variable-name/4385/2 "2022-11-02T00:48:27Z")

</div>

In a shell script, you would want to use export

```auto
export varName="BUILD_NUMBER"

```

The env. portion is just useful inside of the groovy part of the pipeline. Everything in env is exported as an actual environment variable in the shell script.

---

<div class="post-metadata">

**Author:** ![JasonDavis](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@JasonDavis](https://community.jenkins.io/u/JasonDavis)\
**Post date:** [November 2, 2022, 7:40pm UTC](https://community.jenkins.io/t/jenkins-pipeline-script-how-to-use-variable-as-a-variable-name/4385/3 "2022-11-02T19:40:07Z")

</div>

@slide_o_mix

Agree that everything in env is exported to actual environment variables in the shell script

However env. does print expected output

As I had mentioned in my original post echo “${env.BUILD\_ID}” # This prints expected output. Example : 51

After

export varName=BUILD\_NUMBER

echo “${”$varName"}"

prints the output as BUILD\_NUMBER and not acuatl build number

Thank you  
Jason

---

<div class="post-metadata">

**Author:** ![halkeye](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/halkeye/32/8_2.png) [@halkeye](https://community.jenkins.io/u/halkeye)\
**Post date:** [November 2, 2022, 7:42pm UTC](https://community.jenkins.io/t/jenkins-pipeline-script-how-to-use-variable-as-a-variable-name/4385/4 "2022-11-02T19:42:26Z")

</div>

> [@JasonDavis](#):
>
> echo “${”$varName"}"

is that a shell echo or a pipeline echo?

for pipeline, based on [jenkins - Jenkinsfile Declarative Pipeline defining dynamic env vars - Stack Overflow](https://stackoverflow.com/a/73624101) it looks like you can do `env."$varName"` but to me this seems like a code smell, and dangerously close to eval.

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [November 2, 2022, 9:11pm UTC](https://community.jenkins.io/t/jenkins-pipeline-script-how-to-use-variable-as-a-variable-name/4385/5 "2022-11-02T21:11:14Z")

</div>

If your shell script is wrapped in double quotes, then ${env.BUILD\_ID} is being replaced by groovy and not the shell. Can you share your pipeline (redacted)? It would help understand your context better.

---

<div class="post-metadata">

**Author:** ![JasonDavis](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@JasonDavis](https://community.jenkins.io/u/JasonDavis)\
**Post date:** [January 3, 2023, 8:03am UTC](https://community.jenkins.io/t/jenkins-pipeline-script-how-to-use-variable-as-a-variable-name/4385/6 "2023-01-03T08:03:06Z")

</div>

> [@JasonDavis](#):
>
> e above example, I

@halkeye halkeye and @slide_o_mix

Thank you for replies and sorry for the delay in answering your questions  
I solved my requirement through some other means and this thread can be closed

---

<div class="post-metadata">

**Author:** ![poddingue](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/poddingue/32/985_2.png) [@poddingue](https://community.jenkins.io/u/poddingue)\
**Post date:** [January 4, 2023, 9:40am UTC](https://community.jenkins.io/t/jenkins-pipeline-script-how-to-use-variable-as-a-variable-name/4385/7 "2023-01-04T09:40:03Z")

</div>

Thank a lot for your feedback @JasonDavis 👍 .

---

<div class="post-metadata">

**Author:** ![harbulot](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/harbulot/32/3606_2.png) [@harbulot](https://community.jenkins.io/u/harbulot)\
**Post date:** [March 13, 2023, 10:10am UTC](https://community.jenkins.io/t/jenkins-pipeline-script-how-to-use-variable-as-a-variable-name/4385/8 "2023-03-13T10:10:09Z")

</div>

As @halkeye says, there is indeed a code smell. I would in fact suggest that using the `sh` step with Groovy strings interpolation (i.e. strings with double quotes) is generally a code smell. See [the Injection via Interpolation section in the manual](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#injection-via-interpolation), which can be particularly dangerous for shell scripts.

The original question can be more or less solved directly solved by using an immutable string (single quotes). The workaround against Injection via Interpolation is to pass variable via the environment, but this is already the case here.

You can achieve all this variable substitution in a safer way directly in Bash:

```plaintext
script {
    env.BUILD_ID = "1234"
    env.BUILD_NUMBER = "5678"
}

sh '''#!/bin/bash
# The double-quotes here belong to the shell script,
# nothing to do with the Groovy string.
echo "$BUILD_ID"
varName="BUILD_NUMBER"

# Bash will expand this as echo "$BUILD_NUMBER", which will use the environment
# variable defined earlier, inside this shell script or passed to env in the groovy
# script
echo "${!varName}"
'''

```

Note: this way of expanding variables is specific to Bash and might require a different syntax with other shells (hence the `#!/bin/bash` at the start). If needed, you can check the shell running `sh` steps by default using `sh 'readlink /proc/$$/exe'`.
