0

Hello I'm using read in bash to ask for a password and have -s to hide input and -t 10 to timeout and all works as expected except for one thing.
Here is the code (I'm using the array switch):

read -t 10 -a mp -s -p "Enter Password:"

It hides the input and if I don't type anything it returns to prompt in 10 secs.
But if I type first 10 chars of a 12 char pass and it times out well when it returns it shows the chars I typed on the next line.

Example:

DD-WRT-Bash:~# read -t 10 -a mp -s -p "Enter Password:"

(i type testing123)

If I don't hit enter and it times out next line is:

DD-WRT-Bash:~# testing123

It does the same thing in Ubuntu.
Is there anyway to prevent the text from being returned on the next line?

Aaron
  • 24,009
  • 2
  • 33
  • 57
8ts
  • 3
  • 1

1 Answers1

0

I suggest using the -N option in order to circumvent read's behaviour that is to only consume full lines of input.

The description of -N starts as follows :

-N nchars
read returns after reading exactly nchars characters
rather than waiting for a complete line of input, unless
EOF is encountered or read times out.

Using a value large enough that your users wouldn't be able to reach it before the timeout seems to work well :

read -t 10 -a mp -s -N 9999 -p "Enter Password:"

A partial line will be stored into the array and won't be reproduced on your console's next line of input.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • Thank you. That works great. My script works as intended now. Appreciate the fast response. – 8ts Aug 04 '20 at 17:37