title: Escape a space in a grep command inside a bash script
body: <p>I'm trying to do a simple grep like this inside a bash script,</p>
  
  <pre><code>grep "2017/01/01 00:" log.txt | wc -l
  </code></pre>
  
  <p>That works fine.</p>
  
  <p>Inside my bash script I'm trying to do this,</p>
  
  <pre><code>i=16
  DATE="$(date -d $2' '$i:00:00 '+%Y/%m/%d %H')"
  echo $DATE
  COUNT="$(grep $DATE $1 | wc -l)"
  echo $COUNT
  </code></pre>
  
  <p>And I call it like this,</p>
  
  <pre><code>test.sh log.txt 2017/01/01
  </code></pre>
  
  <p>That fails because of the space, with a</p>
  
  <pre><code>: No such file or directory
  </code></pre>
  
  <p>So I tried adding double quotes, single quotes, and many other things, but they all fail...</p>
  
  <pre><code>COUNT="$(grep \"$DATE\" $1 | wc -l)"
  </code></pre>
  
  <p>I also tried using printf, and although this looks promising,</p>
  
  <pre><code>printf -v COMMAND "grep \"$DATE"
  echo $COMMAND
  # outputs: grep "2016/05/25 16
  </code></pre>
  
  <p>this one messes up with the beginning of the string!</p>
  
  <pre><code>printf -v COMMAND "grep \"$DATE\""
  echo $COMMAND
  # outputs: "rep "2016/05/25 16
  </code></pre>
  
  <p>What happened to the "g"??
  How can I escape that space? I also tried replacing it by \s, which works in the command line, but gives wrong results from the bash script.</p>
  
  <p>Really confused.</p>