I am trying to read a single char from stdin inside a shell script (not a bash-script).
I have a script with the following line:
#!/bin/sh
…
char=$(/bin/bash -c 'read -s -n1 key; echo "$key"')
…
This works (assuming bash is installed), but it feels kinda wrong to have that line of code.
Is there a better more portable way to do it?
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.
I also tried char="$(dd bs=1 count=1)"
but dd only returns/exits after a newline/EOF.
Duplicate
This question has answers that work for my problem.
This might not be an exact duplicate because:
- That question is only about ksh.
- The top voted answer is only repeating my problem: "How to do it with bash"
But it was asked and answered, because the excepted and the best answer, both work cross platform. The answer I named "best" does it better ;)