title: Escaping a lengthy Bash command to use with PHP exec()
body: <p>I have a semi-lengthy shell command that I need to execute from within php.  I feel like I have everything properly escaped, however it is not working properly when executed from php's exec() function.  When I run the command with exec() it exits without error, but does not run properly.  </p>
  
  <p>I am comparing a list file that has email addresses with additional pipe delimited information against a text file containing the md5 version of email addresses to suppress from the original list.</p>
  
  <p>Here is the bash command:</p>
  
  <pre><code>while read line ; do email=`echo "$line" | cut -d '|' -f1` ; if [ $(E=`echo -en "$email" | md5sum | awk '{print $1}'`; grep $E /path/to/suppressions | head -1 ;) ] ; then continue ; else echo $line ; fi done &lt; /path/to/emails
  </code></pre>
  
  <p>/path/to/suppressions is a text file, containing the md5 version of an email address to suppress (it contains the md5 for "emailtosuppress@domain.com":</p>
  
  <pre><code>edb5feb3be7d0a4e1e250ccdf0c04ace
  </code></pre>
  
  <p>/path/to/emails is a text file containing a list of email addresses with some other delimeted information:</p>
  
  <pre><code>emailtokeep@domain.com|1000|1
  emailtosuppress@domain.com|1000|1
  </code></pre>
  
  <p>When the command is executed in bash, the output is simply the email address that does NOT exist in the suppression list, and it works flawless:</p>
  
  <pre><code>emailtokeep@domain.com|1000|1
  </code></pre>
  
  <p>The trouble I am having is when I execute this same command with exec() in php, it outputs all the lines from the "emails" file, without suppressing any matches from the suppression file.</p>
  
  <pre><code>$supCommand = 'while read line ; do email=`echo "$line" | cut -d \'|\' -f1` ; if [ $(E=`echo -en "$email" | md5sum | awk \'{print $1}\'`; grep $E /path/to/suppressions | head -1 ;) ] ; then continue ; else echo $line ; fi done &lt; /path/to/emails' ;
  exec($supCommand, $supStatus) ;
  print_r($supStatus) ;
  </code></pre>
  
  <p>I enclosed the command in single quotes, and escaped the four instances of single quotes within the command.  If anyone has any ideas I would be extremely GRATEFUL.</p>
  
  <p>Thanks in advance :)</p>