Issue
I'm writing pipeline in Jenkins. My code looks something like below:
void someFun(){
sh '''
VAR='a_b_c_d'
TEMPVAR=$VAR | tr '_' '-'
echo "With hyphens $TEMPVAR-blah-blah"
echo "With underscores $VAR"
'''
}
stage{
someFun()
}
All I want to achieve is a way to replace underscores from 1st variable and use its value in 2nd variable. Also. I'm not intending to mutate VAR. And I want to store the value, not just print it. When I'm using this above approach, I'm getting TEMPVAR empty.
What I'm trying to possible to achieve is possible? If yes, what is the way to achieve it? I read multiple posts but couldn’t find any helpful:(
Solution
You can do it in many ways, like with:
tr
, but in this case you need to use an additional shell:TEMPVAR="$(echo "$VAR" | tr _ -)"
or even better with string substitution:
TEMPVAR="${VAR//_/-}"
Answered By - Marco Luzzara