Pipeline: Get variable value, when its name is stored in another variable

26 views
Skip to first unread message

Shiran

unread,
Apr 29, 2018, 11:21:35 AM4/29/18
to Jenkins Users
Hi,

I want to get a variable's value, and I have its name stored in another variable:

I need to get the value of FIRST_FOLDER variable:

In bash I would do:
bash-4.2$ export FIRST_FOLDER=path/to/folder
bash-4.2$ export TEMP=FIRST_FOLDER
bash
-4.2$ echo ${!FULL_WORD}
path/to/workspace

Wanted output: path/to/folder

When I try to do it in Jenkins:
script="""
export FIRST_FOLDER=path/to/folder
export TEMP=FIRST_FOLDER
echo \${!FULL_WORD}
"""
sh script

I get:
script.sh: Bad substitution

Please help
Thanks :)

Martin d'Anjou

unread,
Apr 29, 2018, 12:54:22 PM4/29/18
to Jenkins Users
Shiran,

Put #!/bin/bash at the start of the script. By default Jenkins calls sh, which is not the same as bash and sh does not have variable indirection. You can also safely remote the backslash if you use triple single-quotes:
node() {
    script
='''#!/bin/bash
export FIRST_FOLDER=path/to/folder
export TEMP=FIRST_FOLDER
echo ${!TEMP}
'''

sh script
}

Martin

Shiran

unread,
Apr 29, 2018, 1:59:54 PM4/29/18
to Jenkins Users
Thanks!  :) it worked good, but..

My original aim is to do it inside "repo forall" command. I tried this:
script = """#!/bin/bash
export FIRST_FOLDER=path/to/folder
export TEMP=FIRST_FOLDER
echo \${!TEMP}
"""
sh "repo forall -c '${script}' "

but got the same error...
Reply all
Reply to author
Forward
0 new messages