0

I have a Jenkins job with an execute shell box. In the execute shell I use bash instead of dash (sh). In the execute shell I have strings which are supposed to be interpreted as they contain escape sequences (in this case the new line: \n), so here is an example execute shell:

#!/bin/bash
set -x #echo on
git fetch
...

git commit -m "Merging $DEVELOPEMENTBRANCH into $INITIALBRANCH\n\nThis is an example second paragraph."
...

My problem here is that the script block is interpreted/replaced by Jenkins in a way that it changes its behavior, to be specific it replaces the variables and replaces the double quotes with single quotes so in the console output it looks like this:

[4] $ /bin/bash /tmp/hudson7542128982632971668.sh
+ git fetch
...

+ git commit -m 'Merging my_feature into develop\n\nThis is an example second paragraph'
...

But in this way the \n part won't be interpreted because of the single quotes. What is the trick here? I want to preserve the double quotes or at least interpret the \n sequences.

András
  • 683
  • 5
  • 15
  • Riska, can you please try with "\"Merging $DEVELOPEMENTBRANCH into $INITIALBRANCH\n\nThis is an example second paragraph.\"" ? – Shubhangi Jun 02 '16 at 13:53
  • It's not working as it becomes: '"Merging my_feature into develop\n\nThis is an example second paragraph"'. As the single quotes are outside the result will be the same (with an extra pair of double quotes around the message) – András Jun 02 '16 at 14:03
  • For multi-line `git` commit msg have you tried following answers from http://stackoverflow.com/a/5064653/6128602? – luka5z Jun 03 '16 at 06:02
  • Actually, I just used the git commit as a sample command. I use several similar commands with the very same issue and I needed a universal solution for that. – András Jun 03 '16 at 07:42

1 Answers1

0

git commit -m "A multi-line\n\ncommit message" will not produce a multi-line commit message anyway. The commit message will be, literally, A multi-line\n\ncommit message. Double-quotes do not cause bash to interpret printf escape-sequences.

To get a multi-line commit-message you need:

git commit -m "`printf \"A multi-line\n\ncommit message\"`"

This works fine in a Jenkins shell step.

Mike Kinghan
  • 46,463
  • 8
  • 124
  • 156
  • If the shell is bash and not `/bin/sh`, one could avoid the performance overhead of the command substitution by using `git commit -m $'A multi-line\n\ncommit message'`. – Charles Duffy Oct 26 '17 at 00:23