title: BASH MySQL Query to Comma Separated File
body: <p>I have the following bash script:</p>
  
  <pre><code>#!/bin/sh
  
  MYSQLHOST="mysql.remote-host.com"
  MYSQLDB="mysqldb"
  MYSQLTABLE="table"
  MYSQLUSER="user"
  MYSQLPASS="pass"
  MYSQLDUMP="Report/report.csv"
  LOG="Report/report.log"
  
  MYSQLOPTS="--user=${MYSQLUSER} --password=${MYSQLPASS} --host=${MYSQLHOST} ${MYSQLDB}"
  
  
  echo "Report Begin: $(date)"
   echo "MySQL Dump Begin: $(date)" &gt;&gt; ${LOG}
    echo "SELECT ${MYSQLTABLE}.created_at, ${MYSQLTABLE}.product_options FROM ${MYSQLTABLE} WHERE ${MYSQLTABLE}.product_id=1 ORDER BY ${MYSQLTABLE}.created_at" | mysql ${MYSQLOPTS} &gt; ${MYSQLDUMP}
   echo "MySQL Dump End: $(date)" &gt;&gt; ${LOG}
  echo "Report Successful: $(date)"
  </code></pre>
  
  <p>This ouputs my MySQL Query into a "<strong>TAB</strong>" separated file <em>report.csv</em>.  However, I need to have it output to a "<strong>COMMA</strong>" separated file.  I realize I could create another script to convert this file from TAB to COMMA separated, however, I'd rather save the step if I can.  So how can I have MySQL dump the file in comma separated format?</p>
  
  <p>EDIT:
  I did find this solution: <a href="https://stackoverflow.com/questions/707473/how-do-you-output-mysql-query-results-in-csv-format-to-the-screen-not-to-a-file">How do you output MySQL query results in csv format (to the screen, not to a file)?</a></p>
  
  <p>However I can't seem to get it to work:</p>
  
  <pre><code>echo "SELECT CONCAT_WS(',', ${MYSQLTABLE}.created_at, ${MYSQLTABLE}.product_options) FROM ${MYSQLTABLE} WHERE ${MYSQLTABLE}.product_id=1 ORDER BY ${MYSQLTABLE}.created_at" | mysql ${MYSQLOPTS} &gt; ${MYSQLDUMP}
  </code></pre>
  
  <p>Doesn't work :(</p>