johnslattery
8/19/2016 - 6:31 PM

Often it's suggested that the way to provide a complex prompt for the bash read builtin, perhaps including escape sequences and shell variab

Often it's suggested that the way to provide a complex prompt for the bash read builtin, perhaps including escape sequences and shell variables, is to printf it separately. The draw back is that read nicely only uses the prompt if it is called from a terminal. If all you're after is a trailing newline, use ANSI-C quoting as described here: https://www.gnu.org/software/bash/manual/bash.html#ANSI_002dC-Quoting. If you're after more complex formatting, use printf and command substitution, but beware the command substitution strips trailing newlines. Work around this by including a final ANSI-C quoted newline.

read -rsn 1 -p $'Press c to cancel. Any other key to try again.\n' keypress
read -rsn 1 -p "$(printf "Press '%s' to cancel. Any other key to try again." \
  "$key")"$'\n' keypress