title: MySQL Query to CSV
body: <p>Is there an easy way to run a <code>MySQL</code> query from the <code>linux</code> command line and output the results in csv format?</p>
  
  <p>Here's what I'm doing now:</p>
  
  <pre><code>mysql -u uid -ppwd -D dbname &lt;&lt; EOQ | sed -e 's/        /,/g' | tee
  list.csv select id, concat("\"",name,"\"") as name from students EOQ
  </code></pre>
  
  <p>It gets messy when there are a lot of columns that need to be surrounded by quotes, or if there are quotes in the results that need to be escaped.</p>