title: Bash - Escaping single quotes
body: <p>I got a database script that searches for duplicate IDs, when given a certain ID, last name, and first-name. Here are the meat and potatoes of it:</p>
  
  <pre><code>PSQL="psql -p $PGPORT -h $DBHOST -d $DB -tAc "
  
  DUP_ID=$($PSQL "SELECT id FROM inmate WHERE id NOT SIMILAR TO '(0*)${1}' AND lastname ILIKE '${_LNAME}' AND firstname ILIKE '${_FNAME}' LIMIT 1")
  </code></pre>
  
  <p>Works great, except when either the last or first name has an apostrophe in it, such as "O'Neil". I've attempted to escape any instance of ' with \', and have not yet met with success. I've spent all day searching the forums, and tried different variations on my own, but it still will not add a \ in front of every '.</p>
  
  <p>Here's what I got so far:</p>
  
  <pre><code>local _LNAME=`echo "${2}" | sed "s/'/\\\'/g"`
  local _FNAME=`echo "${3}" | sed "s/'/\\\'/g"`
  echo -e $_LNAME
  echo -e $_FNAME
  
  # Output
  
  O'Neil
  Robert
  </code></pre>
  
  <p>As always, thanks in advance!</p>