title: printf is "eating" the backslashes. How to preserve them?
body: <p>I'm trying to make a tiny POSIX-compatible sed-like function. However I noticed that the code below is "eating" the backslashes.</p>
  
  <p><code>input.txt</code> for <code>mysed ./input.txt "TEST2" "TEST2=\"two\""</code> call :</p>
  
  <pre><code>TEST1="\\_PR.CP%02d"
  TEST2="one"
  TEST3="\\_PR.P%03d"
  </code></pre>
  
  <p>Expected:</p>
  
  <pre><code>TEST1="\\_PR.CP%02d"
  TEST2="two"
  TEST3="\\_PR.P%03d"
  </code></pre>
  
  <p>Received:</p>
  
  <pre><code>TEST1="\_PR.CP%02d"
  TEST2="two"
  TEST3="\_PR.P%03d"
  </code></pre>
  
  <p>How to modify the code below to preserve all the backslashes? Maybe it's possible to replace a custom <code>printf '%b\n'</code> format by something better?</p>
  
  <pre><code>#!/bin/sh
  # Replaces the lines containing the text '$2' of a file '$1' with a line '$3'.
  mysed () {
      if [ -f "$1" ] ; then
          mysed_tmp="./.mysed"
          rm -f $mysed_tmp
          while IFS= read -r line
          do
              case $line in
                  *"$2"*)
                      if [ ! -z "$3" ] ; then
                          printf '%b\n' "$3" &gt;&gt; $mysed_tmp
                      fi
                      ;;
                  *)
                      printf '%b\n' "$line" &gt;&gt; $mysed_tmp
                      ;;
              esac
          done &lt; "$1"
          mv $mysed_tmp $1
          return 0
      else
          return 1
      fi
  }
  
  echo "TEST1=\"\\\\_PR.CP%02d\"" &gt; input.txt
  echo "TEST2=\"one\"" &gt;&gt; input.txt
  echo "TEST3=\"\\\\_PR.P%03d\"" &gt;&gt; input.txt
  
  mysed ./input.txt "TEST2" "TEST2=\"two\""
  cat ./input.txt
  </code></pre>
  
  <p><strong>EDIT:</strong> replaced <code>'%b\n'</code> with <code>'%s\n'</code>, everything is working now as intended.</p>