title: Can't index of the "#[hello]" in bash
body: <p>The following <code>index_of</code> function doesn't work for all cases:</p>
  <pre><code>#!/bin/bash
  
  index_of() {
      local string=&quot;$1&quot;
      local search_string=&quot;$2&quot;
      
      local prefix=${string/${search_string}*/}
      
      local index=${#prefix}
       
      if [[ index -eq ${#string} ]];
      then
          index=-1
      fi
      
      printf &quot;%s&quot; &quot;$index&quot;
  }
  
  a='#[hello] world'
  
  b=$(index_of &quot;$a&quot; &quot;world&quot;)
  echo &quot;A: $b&quot;
  
  b=$(index_of &quot;$a&quot; &quot;hello&quot;)
  echo &quot;B: $b&quot;
  
  b=$(index_of &quot;$a&quot; &quot;#[hello]&quot;)
  echo &quot;C: $b&quot;
  </code></pre>
  <p>Here is the output:</p>
  <pre><code>A: 9
  B: 2
  C: -1
  </code></pre>
  <p>The <code>A</code> and <code>B</code> is correct, but the <code>C</code> is incorrect.</p>
  <p>The <code>C</code> should be <code>0</code> instead of <code>-1</code>.</p>
  <p>What's wrong in the <code>index_of</code> function and how to fix the <code>C</code> index?</p>