1

Below is my questions

RELEASE_BRANCH=5.2.2

echo $RELEASE_BRANCH | sed 's/\./\\./g'
5\.2\.2

RELEASE=`echo $RELEASE_BRANCH|sed 's/\./\\./g'`

echo $RELEASE
5.2.2

What I am expecting

echo $RELEASE
5\.2\.2

Any ideas?

2
  • 2
    Have you tried sed 's/\./\\\\./g'? Commented Aug 5, 2014 at 23:46
  • Just a friendly reminder to "accept" an answer once you've gotten one you like. Commented Aug 6, 2014 at 15:58

5 Answers 5

5

Use of back-ticks is deprecated. You should use command substitution instead.

#!/bin/bash

RELEASE_BRANCH=5.2.2
RELEASE=$(echo $RELEASE_BRANCH|sed 's/\./\\./g')

echo "$RELEASE"

Note: Please read the Important differences (bullet 1) from the link for more details.

Sign up to request clarification or add additional context in comments.

Comments

1

If I understand you, then this should give you your expected result -

$ RELEASE=$(echo $RELEASE_BRANCH|sed 's/\./\\./g')
$ echo $RELEASE
5\.2\.2

Comments

1

Since you're using backticks, you need to escape the backslashes one extra time:

RELEASE=`echo $RELEASE_BRANCH|sed 's/\\./\\\\./g'`

Comments

1

You also use this method,

$RELEASE=`echo $RELEASE_BRANCH|sed -r 's/\./\\\&/g'`
$echo $RELEASE
 5\.2\.2

Comments

1

We seldom think about the powerful string-manipulation functions made available by bash, and in this case the following works very well:

${string/substring/replacement}

Replace first match of $substring with $replacement.

${string//substring/replacement}

Replace all matches of $substring with $replacement.

So here we are going to use the second version, to have a neat one-liner:

$ RELEASE_BRANCH=5.2.2
$ echo ${RELEASE_BRANCH//./\\.}
5\.2\.2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.