Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make bash script ask for a password? [closed]

Tags:

bash

passwords

I want to secure the execution of a program with a password. How can I ask the user to enter a password without echoing it?

like image 917
Debugger Avatar asked Apr 16 '10 15:04

Debugger


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 bash script?

bash [filename] runs the commands saved in a file. $@ 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.

How do I make my bash script sleep?

How to Use the Bash Sleep Command. Sleep is a very versatile command with a very simple syntax. It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.


2 Answers

This command will read into var pwd from stdin (with echo disabled):

IFS= read -s  -p Password: pwd 

Unsetting IFS will allow for leading and trailing whitespace in passwords (which may be supported in some environments, so best to support it during your script's input of the user credentials)

To validate leading/trailing whitespace is handled appropriately you can use:

echo -n "$pwd" | hexdump -C 

Note: don't use with real passwords as it dumps to the console!

HT: Ron DuPlain for this additional information on IFS unsetting.

like image 77
Jürgen Hötzel Avatar answered Oct 07 '22 06:10

Jürgen Hötzel


stty_orig=$(stty -g) # save original terminal setting. stty -echo           # turn-off echoing. IFS= read -r passwd  # read the password stty "$stty_orig"    # restore terminal setting. 
like image 39
codaddict Avatar answered Oct 07 '22 04:10

codaddict