title: Can someone explain me why awk's sub() / gsub() works like this?
body: <p>I know <code>awk</code> can do text/string substitution with <code>sub()</code> and <code>gsub()</code> like:</p>
  
  <pre><code>kent$ echo "fffff"|awk '{gsub("f", "b")}1'
  bbbbb
  </code></pre>
  
  <p>or</p>
  
  <pre><code>kent$ echo "fffff"|awk '{gsub(/f/, "b")}1'
  bbbbb
  </code></pre>
  
  <p>However today I made a typo mistake, I wrote the line as:</p>
  
  <pre><code>kent$ echo "fffff"|awk '{gsub('f', "b")}1'
  </code></pre>
  
  <p>But <code>awk</code> didn't complain about that but generated output as usual, of course, unexpected output, it took me sometime to find out the error. The output <code>awk</code> gave me was:</p>
  
  <pre><code>bfbfbfbfbfb
  </code></pre>
  
  <p>another example:</p>
  
  <pre><code>kent$ echo "fafafafafXX"|awk '{gsub('fa', "B")}1'
  BfBaBfBaBfBaBfBaBfBXBXB
  </code></pre>
  
  <p>example with <code>sub()</code> is strange too:</p>
  
  <pre><code>kent$  echo "thanks in advance"|awk '{sub('a', "B")}1'
  Bthanks in advance
  </code></pre>
  
  <p>Could someone explain me how was the strange substitution done? </p>
  
  <pre><code>kent$  awk --version
  GNU Awk 4.0.2
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>thanks for the answer from Joni. maybe this example explains it better, I just add it here:</p>
  
  <pre><code>kent$  echo "thanks in advance"|awk '{f="k";sub('f', "B")}1'
  thanBs in advance
  
  kent$  echo "thanks in advance"|awk '{sub('th ank', "B")}1'
  awk: cmd. line:2: {sub(th
  awk: cmd. line:2:        ^ unexpected newline or end of string
  </code></pre>