How to assign a groovy variable to a shell variable

+5 votes

I have the following code snippet in my Declarative Jenkins pipeline wherein I am trying to assign a groovy array variable 'test_id' to a shell array variable 't_id'. 


def test_id

                  sh """

                  t_id=($(bash -c \" source ./ ${BITBUCKET_COMMON_CREDS_USR} ${BITBUCKET_COMMON_CREDS_PSW} ${env.StoryId} \"))



                   echo "${test_id[0]}"

                   echo "${test_id[1]}"


The shell commands work in the bash shell but when I add the multi line sh step, I am getting the below syntax error when I run it in Jenkins. Can you please help?

WorkflowScript: 46: illegal string body character after dollar sign;
   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 46, column 20.
                     	sh """
Sep 11, 2019 in Jenkins by Abhilash
• 380 points
Hey @Abhilash  I guess the groovy templating is giving you this issue.

Try to use triple single quotes ''' instead of '' '' '' or escape the dollar \$.

Thanks @Sirajul! Escape sequence works. But, currently, I am unable to get the value assigned to the groovy array test_id from the Shell array variable t_id. How can I access it?


def test_id

                  sh """

                  t_id=(\$(bash -c \" source ./ ${BITBUCKET_COMMON_CREDS_USR} ${BITBUCKET_COMMON_CREDS_PSW} ${env.StoryId} \"))

                    echo "\${t_id[*]}" //Returns the array's elements 

                    test_id=\${("\${t_id[@]}")} // throws an error as Bad substitution


                    echo "${test_id[0]}" //Does not return any value

                    echo "${test_id[1]}" //Does not return any value
Hey @Abhilash, Can you access the groovy array variable alone? Try accessing only the groovy array just to be sure if it's correctly declared in the groovy syntax.
Hey @Sirajul, tried the following as you have suggested:

def test_id = []
println test_id.getClass() //class java.util.ArrayList

test_id = ["17372", "17374"]
echo "${test_id[0]}" //returns 17372
echo "${test_id[1]}" //returns 17374

Please let me know your comments.

@Abhilash try adding this array declaration in your script, something like this, you might have missed on groovy array syntax, Let me know if this works, if not I will try replicating this issue on my side.


def test_id = []

                  sh """

                  t_id=(\$(bash -c \" source ./ ${BITBUCKET_COMMON_CREDS_USR} ${BITBUCKET_COMMON_CREDS_PSW} ${env.StoryId} \"))

                    echo "\${t_id[*]}" //Returns the array's elements 



                    echo "${test_id[0]}" 

                    echo "${test_id[1]}" 
@Sirajul, I tried the same as you have suggested except by additionally removing the comment but I am still facing the same 'Bad Substitution' error. Please let me know if you get anything from your end.

See if this could help you @Abhilash This might have something to do with String interpolation I believe. Try adding the shebang line as well, just in case.

Hi Karan,

Thanks for your inputs. But, the string interpolation as provided in the link works for accessing the groovy variable in the shell script. I am actually interested in assigning the groovy array list variable 'test_id' with the shell script array variable 't_id'. I tried it but it is not working. Is there a way we can achieve this?

How to read the  shell variable in groovy / how to assign shell return value to groovy variable. 

Requirement : Open a text file read the lines  using shell and store the value in groovy  and get the parameter for each line .

Here , is delimiter

Ex: releaseModule.txt




Here want to get  module name 2nd Parameter (configurable-wf-report) ,  build no 3rd  Parameter (94), commit id 4th (23crb1)

def  module = sh(script: """awk -F',' '{ print \$2 "," \$3 "," \$4 }' releaseModules.txt  | sort -u """, returnStdout: true).trim()

echo module

List lines = module.split( '\n' ).findAll { !it.startsWith( ',' ) }

def buildid

def Modname

lines.each {

List det1 =  it.split(',')


Modname = det1[0].trim()

tag= det1[2].trim()


echo Modname               

echo buildid

                echo tag




Can you explain your requirement a little bit more? Also, share your complete script?
I had provided the solution, those who want to assign shell array value to groovy variable.

Just pass the shell array value file, and use pipeline, stages, stage, step, script. Inside the script pass my code.

2 answers to this question.

+2 votes
Best answer

I have tried the following in my script and it worked 

stage ('Test about page')
echo '===========Testing============'
test_output = sh (script: 'java -jar Final_test.jar ', , returnStdout:true).trim()
echo 'test passed !!! '
// if the test is successful push the image to docker registry
sh 'sudo docker push smartbond/simple-php-website:v${BUILD_NUMBER}'

Please try the underlined statement to convert the groovy variable to shell script.

<groovy variable> = sh (script: '<shell command> ', , returnStdout:true).trim()

The output is a string and you can assign this to a shellscript ${<variable name>} 

reference :



PS: If this post has helped you, please upvote it 

answered Dec 4, 2019 by Raveendiran
• 980 points

selected Dec 4, 2019 by Kalgi

Hi @Raveendiran! I understand your answer but I have a small doubt. If the command docker push command fails(I always end up with errors while pushing an image to the Docker registry), what will be the value of "test_result"?

0 votes


To use a template string, where variables are substituted into a string, use double-quotes. So here, you can use \$ only for shell environment variables and use $ for groovy variables.

$ sh "echo \$some_var" 
answered Dec 14, 2020 by MD
• 95,420 points

Related Questions In Jenkins

0 votes
2 answers

how to execute a shell script post build step in Jenkins?

You could use the Post Build Task Plugin ...READ MORE

answered Jul 31, 2019 in Jenkins by Sirajul
• 59,210 points
+1 vote
1 answer

Unable to extract elements from a ArrayList and assign to a variable

I got the solution for this: pipeline ...READ MORE

answered Sep 5, 2019 in Jenkins by Abhilash
• 380 points
+4 votes
7 answers

How to set the pipeline name and description for a Jenkinsfile?

You can rename pipeline jobs in jenkins ...READ MORE

answered Oct 11, 2018 in Jenkins by Hannah
• 18,570 points
0 votes
2 answers

How to start Jenkins on a different port rather than 8080?

In case you want to change the ...READ MORE

answered Jul 31, 2019 in Jenkins by Sirajul
• 59,210 points
+5 votes
3 answers

How to trigger a Jenkins pipeline A in another Jenkins pipeline B?

You can run the following code, its ...READ MORE

answered Jul 5, 2018 in Jenkins by Sophie may
• 10,590 points
+2 votes
4 answers
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
+2 votes
1 answer
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP