Adding a character after a digit and dot in bash

You may use:

sed 's/[0-9]\./&\\/g' <<< "$branch"
3.\2.\5

In case you are ok with awk, could you please try following, written and tested with shown samples in link https://ideone.com/T1suTg

echo "$branch" | awk 'BEGIN{FS=".";OFS=".\\"} {$1=$1} 1'

Explanation: Printing shell variable branch value with echo and sending its output as standard input to awk command. In awk program in BEGIN block setting field separator as . and setting output field separator as .\\ which is actually .\ Then in main program re-setting 1st field to itself so that new value of output field separator get applies. 1 will print value of current line.


Also, it is possible to use POSIX BRE expression with sed to insert \ between a dot and a digit:

branch="3.2.5"
firstbranch=$(echo $branch | sed 's/\(\.\)\([[:digit:]]\)/\1\\\2/g') && echo $firstbranch

Result: 3.\2.\5

See online proof.

Regex Explanation

--------------------------------------------------------------------------------
  \(                        group and capture to \1:
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
  \)                        end of \1
--------------------------------------------------------------------------------
  \(                        group and capture to \2:
--------------------------------------------------------------------------------
    [[:digit:]]              any character of: digits (like \d)
--------------------------------------------------------------------------------
  \)                        end of \2

Tags:

Shell

Bash

Awk

Sed