title: Different results between ps aux and `ps aux` inside a script
body: <p>I have a bash script (<em>ScreamDaemon.sh</em>) inside which a check that example of it isn't running already is added.</p>
  
  <pre><code>numscr=`ps aux | grep ScreamDaemon.sh | wc -l`;
  if [ "${numscr}" -gt "2" ]; then
    echo "an instance of ScreamDaemon still running";
    exit 0;
  fi
  </code></pre>
  
  <p>Normally, if there are no another copy of script running, <strong>ps aux | grep ScreamDaemon.sh | wc -l</strong> should return <strong>2</strong> (it should find itself and <em>grep ScreamDaemon.sh</em>), but it returns <strong>3</strong>.</p>
  
  <p>So, I try to analyse what happens and after adding some echoes see this:</p>
  
  <p>there are lines I have added into the script</p>
  
  <pre><code>ps aux | grep ScreamDaemon.sh
  ps aux | grep ScreamDaemon.sh | wc -l
  str=`ps aux | grep ScreamDaemon.sh`
  echo $str
  numscr=`ps aux | grep ScreamDaemon.sh | wc -l`;
  echo $numscr
  </code></pre>
  
  <p>there is an output:</p>
  
  <pre><code>pamela   27894  0.0  0.0 106100  1216 pts/1    S+   13:41   0:00 /bin/bash ./ScreamDaemon.sh
  pamela   27899  0.0  0.0 103252   844 pts/1    S+   13:41   0:00 grep ScreamDaemon.sh
  2
  pamela 27894 0.0 0.0 106100 1216 pts/1 S+ 13:41 0:00 /bin/bash ./ScreamDaemon.sh pamela 27903 0.0 0.0 106100 524 pts/1 S+ 13:41 0:00 /bin/bash ./ScreamDaemon.sh pamela 27905 0.0 0.0 103252 848 pts/1 S+ 13:41 0:00 grep ScreamDaemon.sh
  3
  </code></pre>
  
  <p>I also tried to add the <strong>sleep</strong> command right inside <strong>`ps aux | grep ScreamDaemon.sh; sleep 1m`</strong> and see from the parallel terminal how many instances <strong>ps aux|grep ScreamDaemon.sh</strong> shows:</p>
  
  <pre><code>[pamela@pm03 ~]$ ps aux | grep ScreamDaemon.sh
  pamela   28394  0.0  0.0 106100  1216 pts/1    S+   14:23   0:00 /bin/bash ./ScreamDaemon.sh
  pamela   28403  0.0  0.0 106100   592 pts/1    S+   14:23   0:00 /bin/bash ./ScreamDaemon.sh
  pamela   28408  0.0  0.0 103252   848 pts/9    S+   14:23   0:00 grep ScreamDaemon.sh
  </code></pre>
  
  <p>So, it seems that 
      <strong>str=`ps aux | grep ScreamDaemon.sh`</strong>
  contrary to
      <strong>ps aux | grep ScreamDaemon.sh</strong>
  found two instances of <em>ScreamDaemon.sh</em>, but why? Where this additional copy of <em>ScreamDaemon.sh</em> come from?</p>
  
  <p>This is an output of pstree -ap command</p>
  
  <pre><code>  │   ├─sshd,27806
    │   │   └─sshd,27808
    │   │       └─bash,27809
    │   │           └─ScreamDaemon.sh,28731 ./ScreamDaemon.sh
    │   │               └─ScreamDaemon.sh,28740 ./ScreamDaemon.sh
    │   │                   └─sleep,28743 2m
  </code></pre>