1

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:(

curious_coder
  • 99
  • 1
  • 7

1 Answers1

0

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//_/-}"
    
Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42
  • Thank you, Marco! Just a stupid question over here. In the 1st approach, will it also print value of VAR as we're echoing it? – curious_coder Aug 30 '21 at 09:01
  • You are welcome! No, the output is piped and stays inside the subshell. – Marco Luzzara Aug 30 '21 at 09:49
  • @marco-luzzara, This really deserves an explanation as to why it's not working as OP expected in the post, rather than just "here's the answer and an alternative. – Ian W Aug 30 '21 at 22:09