Using read without triggering a newline action on terminal

Another solution to keep the cursor on the same line after echo could be to use \c escape character along with -e flag.

echo -e "Want to do something fun? (y/n) \c"
read -r
echo "You answered: $REPLY"

Note: It might be specific to some versions of echo: https://stackoverflow.com/a/7154820/2110909


The Enter that causes read to return necessarily moves the cursor to the next line. You need to use terminal escapes to get it back to the previous line. And the rest of your script has some problems anyway. Here's something that works, it should give you a better starting point:

#!/bin/bash -e

PROMPT=">"
while read -p "${PROMPT}" line; do
        echo -en "\033[1A\033[2K"
        echo "You typed: $line"
done  

\033 is an Esc; the \033[1A moves the cursor to the previous line, \033[2K erases whatever was on it.