2

I am trying to setup sh command as part of my Jenkins pipeline. This command has one parameter where list of values is supplied. Those values have spaces in them.

I have tried so many things, arrays, escaping etc etc none worked.

Here is an example:

# In Jenkins task I configure env variable:
VALUE_LIST=("Value One" "Value Two" "Value Three")

# In pipeline:
...
sh "command --param ${env.VALUE_LIST[@]}"
...

Please understand that this is only one of the ways I tried to make this work.

I need the result command to look like:

command --param "Value One" "Value Two" "Value Three"

Best I could achieve was

command --param Value One Value Two Value Three

Which obviously does not work

  • What is your queston? – Michael D. Feb 2 '17 at 12:17
  • 1
    My question is, how do I create this command that will work as expected – Tom Feb 2 '17 at 12:18
  • have you tried single quotes 'instead of double quotes? – Michael D. Feb 2 '17 at 12:20
  • Yes, I honestly tried pretty much anything I could find... sh ' comand --param '"${env.VALUE_LIST[@]} no luck – Tom Feb 2 '17 at 12:23
2

This should work.

printf "%s\0" "${VALUE_LIST[@]}" | xargs -0  sh -c 'command --param "$@"' command_name

Print the values to xargs and use null as delimiter. Then xargs calls sh and sh executes the command with the values as arguments. The last piece, command_name, will represent the command by the name you provide. It is obligatory in this construct, as otherwise command will not receive the first value form the list "$@".

  • @Kusalananda Full enough? – Tomasz Feb 4 '17 at 12:24
  • It did work as expected, but still didn't work in pipeline due to the command it self.. There is some bug in it, I reported it. Fortunately the command accepts parameters in different formats, one of which is JSON and I managed to get it work with that. – Tom Feb 8 '17 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.