Bash read backspace button behavior problem

前端 未结 2 944
情深已故
情深已故 2021-01-05 08:10

When using read in bash, pressing backspace does not delete the last character entered, but appears to append a backspace to the input buffer. Is there any way I can change

相关标签:
2条回答
  • 2021-01-05 08:52

    I know the post is old, still this can be useful for someone. If you need specific response to a single keypress on backspace, something like this can do it (without -e):

    backspace=$(cat << eof
    0000000 005177
    0000002
    eof
    )
    read -sn1 hit
    [[ $(echo "$hit" | od) = "$backspace" ]] && echo -e "\nDo what you want\n"
    
    0 讨论(0)
  • 2021-01-05 08:57

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

    $ read -n10 -e VAR
    

    BTW, nice idea, although I would leave the end of the word to the user (it's a knee-jerk reaction to press return).

    0 讨论(0)
提交回复
热议问题