Bash read backspace button behavior problem

When using reading in bash, clicking on the backspace does not delete the last character typed, but appears to add backspace to the input buffer. Is there a way to change it to remove it removes the last key entered from input? If so, how?

Here is a short example of prog that I use it with if it helps:

#!/bin/bash colour(){ #$1=text to colourise $2=colour id printf "%s%s%s" $(tput setaf $2) "$1" $(tput sgr0) } game_over() { #$1=message $2=score printf "\n%s\n%s\n" "$(colour "Game Over!" 1)" "$1" printf "Your score: %s\n" "$(colour $2 3)" exit 0 } score=0 clear while true; do word=$(shuf -n1 /usr/share/dict/words) #random word from dictionary word=${word,,} #to lower case len=${#word} let "timeout=(3+$len)/2" printf "%s (time %s): " "$(colour $word 2)" "$(colour $timeout 3)" read -t $timeout -n $len input #read input here if [ $? -ne 0 ]; then game_over "You did not answer in time" $score elif [ "$input" != "$word" ]; then game_over "You did not type the word correctly" $score; fi printf "\n" let "score+=$timeout" done 
+7
bash backspace
source share
1 answer

The -n nchars turns the terminal into raw mode, so it is best to rely on readline (-e) [docs] :

 $ read -n10 -e VAR 

By the way, a good idea, although I would leave the end of the word to the user (this is the reaction of the knee reflex to pressing return).

+11
source share

All Articles