title: Bash: Find word in string
body: <p>Is there any way to accomplish this <strong>without</strong> using external programs like <code>sed</code>, <code>grep</code>, <code>awk</code> or the <code>[[ ]]</code> construct and without using nested loops?</p>
  
  <p>Basically, I would like to loop over the provided arguments(words) and check whether it's in the string.</p>
  
  <p>By word, I mean actual tokens (IFS separated strings).</p>
  
  <p>So, find word3 in string <code>word1 word2 word3 word4</code></p>
  
  <p>The only crappy solution I could come up with is:</p>
  
  <pre class="lang-bash prettyprint-override"><code>string="word1 word2 word3 word4"
  
  # attempt to remove diff combos of
  # space + word
  for i in $@; do
      [ "${string/ $i / }" == "$string" ] &amp;&amp;
      [ "${string/#$i / }" == "$string" ] &amp;&amp;
      [ "${string/% $i/ }" == "$string" ] &amp;&amp;
      echo "$i not found" &amp;&amp; continue
  
  
      # if found, actually remove
      # again attempting diff combos
      string="${string/ $i / }"
      string="${string/#$i / }"
      string="${string/% $i/ }"
  done
  
  echo "$string"
  </code></pre>
  
  <p>This should give:</p>
  
  <pre class="lang-bash prettyprint-override"><code>~$ ./script word1 word3 word5
  
  word5 not found
  word2 word4
  </code></pre>