passing variable to bash script in a jenkins pipeline job

23,968 views
Skip to first unread message

Nabil Ghodbane

unread,
Oct 29, 2016, 9:12:18 PM10/29/16
to Jenkins Users

dear experts,


I have a Jenkins pipeline job in which I configure my environment with a bash script named setup.sh which looks like:

#!/bin/bash
export ARCH=$1
echo "architecture = " ${ARCH}


In the Jenkins pipeline script, Icall the setup.sh script with:


def lib_arch
='linux-ubuntu-14.04-x86_64-gcc4.8.4' sh ". /opt/setup.sh ${lib_arch}"


unfortunately it seems that NO variable is passed to the setup.sh script, and the echo ${ARCH} return an empty string!

In addition, I tried to instead do:

sh "source /opt/setup.sh ${lib_arch}"

but this fails as well with the "source not found" message. I also tried changing the first line of my script to

#!/bin/sh

but it does not help.

So how can I pass a parameter to my bash script in a Jenkins pipeline script?

thanks for your help.

Daniel Beck

unread,
Oct 30, 2016, 12:17:06 AM10/30/16
to jenkins...@googlegroups.com

> On 29.10.2016, at 23:12, Nabil Ghodbane <nabil.g...@gmail.com> wrote:
>
> So how can I pass a parameter to my bash script in a Jenkins pipeline script?

You will find that this also doesn't work when you run it from a local command line (a basic first troubleshooting step), so it is entirely unrelated to Jenkins or Pipeline.

Nabil Ghodbane

unread,
Oct 30, 2016, 5:53:27 PM10/30/16
to Jenkins Users, m...@beckweb.net
Hi,
thanks for your "feedback'. Well, it runs well on a local command line... On the other hand, it seems there is a serious issue passing parameters to bash scripts from sh pipeline command

Baptiste Mathus

unread,
Oct 30, 2016, 8:07:09 PM10/30/16
to jenkins...@googlegroups.com
Weird, what does the following print?
def lib_arch='linux-ubuntu-14.04-x86_64-gcc4.8.4'
echo lib_arch
echo ". /opt/setup.sh ${lib_arch}"
echo ". /opt/setup.sh $lib_arch"

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/537fa03f-d87e-4df6-bf23-68e5d9a82db5%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Daniel Beck

unread,
Oct 31, 2016, 12:54:50 PM10/31/16
to jenkins...@googlegroups.com

> On 30.10.2016, at 18:53, Nabil Ghodbane <nabil.g...@gmail.com> wrote:
>
> Well, it runs well on a local command line... On the other hand, it seems there is a serious issue passing parameters to bash scripts from sh pipeline command

I tried both, and you're right -- it does work in a local shell.

However, it also works for me in Pipeline.

Pipeline script (with different path):
----
node {
def lib_arch='linux-ubuntu-14.04-x86_64-gcc4.8.4'
sh ". /Users/danielbeck/foo.sh ${lib_arch}"
}
----

Shell script:
----
#!/bin/bash
echo $@
----

Output (excerpt):
----
[Pipeline] sh
[foo] Running shell script
+ . /Users/danielbeck/foo.sh linux-ubuntu-14.04-x86_64-gcc4.8.4
++ echo linux-ubuntu-14.04-x86_64-gcc4.8.4
linux-ubuntu-14.04-x86_64-gcc4.8.4
[Pipeline] }
----

So something else seems to be going on on your system. Could you provide a complete, minimal reproduction case, and whether your Pipeline plugins are up to date and what the Jenkins version is? Also, did you specify a custom shell executable in the global Jenkins configuration? What shell implementation is taking care of `sh` on your system? Bash?

Siddhesh Malpani

unread,
Mar 12, 2020, 1:52:03 PM3/12/20
to Jenkins Users
Nabil,

You may try this in your shell script:

#!/bin/bash
export ARCH=$1
echo "architecture = " $ARCH

Braces around the 'ARCH' variable is not required.
Although it's too late to answer, it might help someone else.

Jérôme Godbout

unread,
Mar 12, 2020, 2:11:04 PM3/12/20
to jenkins...@googlegroups.com

There is an extra space between your (.) and your opt/setup.sh.

If the variabel come from Jenkins pipeline script, you need double quote (“) to get evaluate, single quote (‘) won’t. You should also pass the value as a string and escape it for your argument something like this:

 

def lib_arch='linux-ubuntu-14.04-x86_64-gcc4.8.4'

sh(script: "./opt/setup.sh \”${lib_arch}\”");

 

That should normally work just fine.

--

You received this message because you are subscribed to the Google Groups "Jenkins Users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/2f3a7f54-d7bf-42ac-bc3e-fc967bc76779%40googlegroups.com.

lawrence wang

unread,
Sep 1, 2021, 4:58:41 PM9/1/21
to Jenkins Users
It's super late but I run into this scenario yesterday.
So below code works, it's passing a boolean variable to shell script

steps {
    script{
        def variable = true
        sh """
        ./command.sh ${variable}
        """
    }
}
Reply all
Reply to author
Forward
0 new messages