Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i stop text showing up when user types during execution of bash script?

Tags:

linux

bash

shell

You can see what problem I'm having just by doing sleep 10 and typing.

How would I go about stopping that typing from showing?

like image 493
Kevin Char Avatar asked Sep 02 '25 15:09

Kevin Char


1 Answers

To turn off keyboard echo, you can use stty -echo. To turn it back on you can do stty echo. To be safe, you would save the stty state and restore it at the end; however if you're reading a password, bash has a built-in -s option to read.

In summary:

stty -echo
# keyboard input is not echoed
stty echo
# keyboard input is echoed

More complete:

state=$(stty -g)
stty -echo
# keyboard is not echoed
stty "$state"
# state is restored.

Better, if you're just asking for a password, and using bash:

read -s password
# password is now in the variable $password; no stty shenanigans
like image 174
Petesh Avatar answered Sep 05 '25 06:09

Petesh