Search code examples
bashtput

How to clear read input when using tput rc?


$ cat test.sh
#!/bin/bash

sc=$(tput sc)
rc=$(tput rc)

printf "$sc%s\n" "Type word"
read -r word
printf "$rc%s\n" "Type word (again)"
read -r word_confirmation

$ ./test.sh
Type word (again)
test

I would like test to be cleared.


Solution

  • tput ed clears the screen from the current cursor position to the end of the screen (broken on macOS, see this answer).

    #!/bin/bash
    
    tput sc
    printf "%s\n" "Type word"
    read -r word
    tput rc
    tput ed
    printf "%s\n" "Type word (again)"
    read -r word_confirmation
    

    tput clear screen