title: How can I read a single char from stdin without invoking bash?
body: <p>I am trying to read a single char from stdin inside a shell script (not a bash-script).</p>
  
  <p>I  have a script with the following line:</p>
  
  <pre><code>#!/bin/sh
  
  …
  
  char=$(/bin/bash -c 'read -s -n1 key; echo "$key"')
  
  …
  </code></pre>
  
  <p>This works (assuming bash is installed), but it feels kinda wrong to have that line of code.</p>
  
  <p>Is there a better more portable way to do it?</p>
  
  <hr>
  
  <p>It should work cross-platform, but even with the few platforms I tested it on (debian, maemo, cygwin, fedora), I could not find a way to do it without having bash installed and invoking an instance of bash just to read one keystroke.</p>
  
  <p>I also tried <code>char="$(dd bs=1 count=1)"</code> but dd only returns/exits after a newline/EOF.</p>
  
  <hr>
  
  <p><em>Duplicate</em></p>
  
  <p>This <a href="https://stackoverflow.com/questions/8725925/how-to-read-just-a-single-character-in-shell-script">question</a> has answers that work for my problem.</p>
  
  <p>This might not be an exact duplicate because:</p>
  
  <ol>
  <li>That question is only about ksh.</li>
  <li>The top voted answer is only repeating my problem: "How to do it with bash"</li>
  </ol>
  
  <p>But it was asked and answered, because the <a href="https://stackoverflow.com/a/8732057/10822366">excepted</a> and the <a href="https://stackoverflow.com/a/30022297/10822366">best</a> answer, both work cross platform. The answer I named "best" does it better ;)</p>