Sample Header Ad - 728x90

Can I read a single character from stdin in POSIX shell?

10 votes
3 answers
7073 views
Only read -r is [specified by POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html) ; read -n NUM, used to read NUM characters, is not. Is there a portable way to automatically return after reading a given number of characters from stdin? My usecase is printing prompts like this: Do the thing? [y/n] If possible, I'd like to have the program automatically proceed after typing y or n, without needing the user to press enter afterwards.
Asked by ash (730 rep)
Aug 26, 2018, 03:34 PM
Last activity: Dec 24, 2023, 08:42 AM