title: Bash script kill command in for loop
body: <p>I want to kill all processes containing some string. I wrote script for doing this. However, when I execute it, it gets "Killed" signal after first iteration of for loop. This is my code:</p>
  
  <pre><code>#!/bin/bash
  
  executeCommand () {
   local pname="$1";
   echo $HOSTNAME;
   local  search_terms=($(ps aux | grep $pname | awk '{print $2}'))
   for pros in "${search_terms[@]}";  do
       kill -9 "$pros"
       echo $pros
   done
  exit
  }
  
  executeCommand "$1"  # get the string that process to be killed contains
  </code></pre>
  
  <p>I execute it like <code>./my_script.sh zookeeper</code>.
  When I delete the line containing <code>kill</code> command, <code>for loop</code> executes until end, otherwise, after first <code>kill</code> command, I get as an output "<code>Killed</code>" and program exits.</p>
  
  <p>What is possible reason for this, and any other solution to reach my goal?</p>