Print asterisk instead of password (Bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print asterisk instead of password (Bash)
# 1  
Old 01-18-2017
Print asterisk instead of password (Bash)

OS : RHEL 6.5
Shell : Bash

With the following bash shell script, when I enter password, it won't be printed in the screen.
But, I would like Asterisk character to be printed instead of the real characters. Any idea how ?

Code:
$ cat pass.sh
echo "Enter the username"
read username

echo "Enter password"
stty -echo
read password
stty echo



Executing :

Code:
$ ./pass.sh
Enter the username
apuser
Enter password
$

# 2  
Old 01-18-2017
Hello John K,

Kindly use read -p option for same, it should do the trick.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-18-2017
I think that the -p flag of read takes the next item as a prompt, so you might have:-
Code:
$ cat pass.sh
echo "Enter the username"
read username

stty -echo
read -p "Enter password:- " password
stty echo
echo           # Force new-line because otherwise
               # the cursor is left at the end of the prompt and may confuse any subsequent output.

To display one * for every key press will be more tricky. You would need to read the input 1 character at a time and display the *, building up your overall password variable as you go. No doubt you would also want to handle mistakes in the input, so you would have to allow a backspace character and both delete an * from the screen (making sure that you don't start deleting the prompt) but also remove the last character from the variable you are building up.

Is it really worth this much effort? A telnet or ssh login prompt does not go to these lengths, and it's of limited value to the user anyway.

Those horrible project management phrases are clanging in my head - cost justification; business need; supportability; ..........



Robin
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 01-18-2017
Hi.

See also:
Code:
ledit (1)            - line editor, version 2.03
rlfe (1)             - "cook" input lines for other programs using readline
rlwrap (1)           - readline wrapper

For some details on readline, see man page for:
Code:
readline (3readline) - get a line from a user with editing

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To print diamond asterisk pattern based on inputs

I have to print the number of stars that increases on each line from the minimum number until it reaches the maximum number, and then decreases until it goes back to the minimum number. After printing out the lines of stars, it should also print the total number of stars printed. I have tried... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

2. Shell Programming and Scripting

Make a password protected bash script resist/refuse “bash -x” when the password is given

I want to give my long scripts to customer. The customer must not be able to read the scripts even if he has the password. The following command locks and unlocks the script but the set +x is simply ignored. The code: read -p 'Script: ' S && C=$S.crypt H='eval "$((dd if=$0 bs=1 skip=//|gpg... (7 Replies)
Discussion started by: frad
7 Replies

3. UNIX for Dummies Questions & Answers

space-asterisk in bash killed my server

So, here's the deal... I was attempting to type this: > grep -R "searchterm" * and somehow I typed this instead: > grep -R "searchterm" > * I accidentally typed "space-asterisk" on the second prompt. This apparently caused Bash to attempt to run the first file in the pwd, which... (1 Reply)
Discussion started by: treesloth
1 Replies

4. Shell Programming and Scripting

Use asterisk in shell script bash

Hello, I am trying to save in a file a single "*" but its not working... look what i am doing... FILE="/home/teste/a.txt" ...BEGIN... ASTERISK="*" echo "STRING $ASTERISK STRING" >> $FILE ...END... when i do it, the result is a list of all files of the current... (4 Replies)
Discussion started by: diogooute
4 Replies

5. Shell Programming and Scripting

how to print asterisk without its wild card functionality

I have three cron entries in a file /cron_entries as 15 * * * * /bin/hourjobs > /tmp/hrjob.log 2>&1 .................. .................... I am trying to read this file in for loop using code below: cron=`cat /cron_entries` for line in $cron do printf... (4 Replies)
Discussion started by: sudh
4 Replies
Login or Register to Ask a Question