title: Why is the execution of my binary search script not proceeding after input?
body: <p>I wrote a shell script for binary search of integers. It's accepting space separated integer array, but when the code proceeds and we input the element to be searched, the code doesn't proceed further. It is either stuck or it is accepting more inputs, both of which is undesired.</p>
  <p>Here's the code:</p>
  <pre><code>#!/bin/bash
  
  declare -a arr
  echo &quot;Enter space separated sorted integers:&quot;
  read -ra arr
  
  declare -i search
  echo -n &quot;Enter the element to be searched for: &quot;
  read -r search
  
  index=-1
  beg=0
  mid=0
  last=${#arr[@]}
  last=$((last - 1))
  
  while [ $beg -le $last ]; do
      mid=$((beg / 2 + end / 2))
      if [ $mid -eq &quot;$search&quot; ]; then
          index=$mid
          break
      elif [ $mid -gt &quot;$search&quot; ]; then
          end=$((mid - 1))
      elif [ $mid -lt &quot;$search&quot; ]; then
          beg=$((mid + 1))
      fi
  done
  
  if [ $index -ne -1 ]; then
      echo &quot;Element found at $index&quot;
  else
      echo &quot;Element not found&quot;
  fi
  </code></pre>