title: bash scripting - read single keystroke including special keys enter and space
body: <p>Not sure if I should put this on stackoverflow or unix.stackexchange but I found some <a href="https://stackoverflow.com/questions/2612274/bash-shell-scripting-return-key-enter-key">similar</a> <a href="https://stackoverflow.com/questions/15712430/bash-read-escaped-characters-and-normal-characters">questions</a> here, so here it goes.</p>
  
  <p>I'm trying to create a script to be called by .bashrc that allows me to select one of two options based on a single keystroke. That wouldn't be hard normally but I want the two keys corresponding to the two options to be space and enter.</p>
  
  <p>Here's what I got so far:</p>
  
  <pre><code>#!/bin/bash
  
  SELECT=""
  while [[ "$SELECT" != $'\x0a' &amp;&amp; "$SELECT" != $'\x20' ]]; do
      echo "Select session type:"
      echo "Press &lt;Enter&gt; to do foo"
      echo "Press &lt;Space&gt; to do bar"
      read -s -N 1 SELECT
      echo "Debug/$SELECT/${#SELECT}"
      [[ "$SELECT" == $'\x0a' ]] &amp;&amp; echo "enter" # do foo
      [[ "$SELECT" == $'\x20' ]] &amp;&amp; echo "space" # do bar
  done
  </code></pre>
  
  <p>The following output is what I get if I press enter, space, backspace and x:</p>
  
  <pre><code>:~$ bin/sessionSelect.sh
  Select session type:
  Press &lt;Enter&gt; to start/resume a screen session
  Press &lt;Space&gt; for a regular ssh session
  Debug//0
  Select session type:
  Press &lt;Enter&gt; to start/resume a screen session
  Press &lt;Space&gt; for a regular ssh session
  Debug//0
  Select session type:
  Press &lt;Enter&gt; to start/resume a screen session
  Press &lt;Space&gt; for a regular ssh session
  Debug//1
  Select session type:
  Press &lt;Enter&gt; to start/resume a screen session
  Press &lt;Space&gt; for a regular ssh session
  Debug/x/1
  </code></pre>
  
  <p>So both enter and space result in an empty SELECT. No way to distinguish the two. I tried to add -d 'D' to the read options, but that didn't help. Maybe someone can point me in the right direction.</p>
  
  <p>The bash version would be 4.2 btw.</p>