title: Bash and PHP, quoting command line arguments
body: <p>I have a PHP program that uses a Bash script to convert a pdf.   However if the filename contains spaces it is not passed through the bash script correctly.</p>
  
  <p>How do you escape filenames with spaces within a bash script?  Do you have to do something special to quote the filename for the "OUTFILE" variable?</p>
  
  <p><strong>Bash script:</strong></p>
  
  <pre><code>#!/bin/bash
  
  INFILE=$1
  OUTFILE=${INFILE%.*}
  
  gs \
  -q \
  -dSAFER \
  -dBATCH \
  -dNOPAUSE \
  -sDEVICE=png256 \
  -r150x150 \
  -sOutputFile=${OUTFILE}.png \
  ${INFILE}
  </code></pre>
  
  <p><strong>PHP script:</strong></p>
  
  <pre><code>echo "converting: ".$spool.$file . "\n";
  system("/home/user/bin/pdf2png.sh " . escapeshellarg($spool . $file));
  </code></pre>
  
  <p><strong>Edit:</strong>  I removed the quotes around the escapeshellarg() variable.  This however did not fix the problem.  Which I think is in the Bash script OUTFILE variable.</p>