views:

44

answers:

3

I have a script that automates a process that needs access to a password protected system. The system is accessed via a command-line program that accepts the user password as an argument. I would like to prompt the user to type in his/her password, assign it to a shell variable, and then use that variable to construct the command line of the accessing program (which will of course produce stream output that I will process). I am a reasonably competent shell programmer in Bourne/Bash, but I don't know how to accept the user input without having it echo to the terminal (or maybe having it echoed using '*' characters). Can anyone help with this?

A: 

Turn echo off using stty, then back on again after.

Ignacio Vazquez-Abrams
+3  A: 
#!/bin/bash
stty -echo
printf "Password: "
read PASSWORD
stty echo
printf "\n"
thecloud
+4  A: 
#!/bin/bash
# Read Password
echo -n Password: 
read -s password
echo
# Run Command
echo $password

Here is another way to do it. The read -s will turn off echo for you. Just replace the echo on the last line with the command you want to run.

wsware
Some shells allow you to specify the prompt for the `read` command: `read -s -p "Password:" password`
Gordon Davisson