Bash: how to get a password without displaying it on the terminal

Posted on December 7, 2007
Filed Under bash |

In bash scripting to require user input we must use the read function. For example to read an username we would make something like this:

  1. read -p "Give me the username " username;

But what about passwords? Using the read as shown, the password will be readable on the terminal emulator and we just do not want this. So how to proceed? Well, we need to stop the output to come on the terminal while typing own password and restart it when we have done. To achieve this we can use the command stty that, how its manpage say, change the terminal settings.

So, own script will become:

  1. stty -echo #disabling tty output
  2. read -p "Give me your password: " password
  3. stty echo #reenable echo

Comments

One Response to “Bash: how to get a password without displaying it on the terminal”

  1. Mikachu on January 19th, 2008 3:47 am

    or you can just use
    read -sp “Give me your password: ” password
    :)

Leave a Reply