Jenkins shell string quotation replacement

2019-09-16 06:15发布

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.

1条回答
小情绪 Triste *
2楼-- · 2019-09-16 06:43

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.

查看更多
登录 后发表回答