Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a password from a shell script without echoing

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 their 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?

like image 243
BD at Rivenhill Avatar asked Oct 20 '10 17:10

BD at Rivenhill


People also ask

How do I prompt a password in bash?

#!/bin/bash echo "Enter Username : " # read username and echo username in terminal read username echo "Enter Password : " # password is read in silent mode i.e. it will # show nothing instead of password. read -s password echo echo "Your password is read in silent mode."

What is $@ in a shell script?

$@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What is $0 $1 in shell script?

$0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1) $9 is the ninth argument.


2 Answers

Here is another way to do it:

#!/bin/bash # Read Password echo -n Password:  read -s password echo # Run Command echo $password 

The read -s will turn off echo for you. Just replace the echo on the last line with the command you want to run.

like image 143
wsware Avatar answered Sep 27 '22 21:09

wsware


A POSIX compliant answer. Notice the use of /bin/sh instead of /bin/bash. (It does work with bash, but it does not require bash.)

#!/bin/sh stty -echo printf "Password: " read PASSWORD stty echo printf "\n" 
like image 36
thecloud Avatar answered Sep 27 '22 22:09

thecloud