1

I have a script which takes input from user for password. After the password prompt, what ever command I run in the next line, the output of that command is printed at the password prompt. How to avoid printing the output at password prompt.

code:

echo -n "Enter Password:"
read -s password

Expected output:

Enter Password:
Login successful.

Current output:

Enter Password:Login successful.
2
  • Social enginnering - faking login screen. ;) Just use read -rsp 'Password:' pass ; echo Commented Apr 3, 2017 at 17:27
  • Thank you, both the answers worked. Commented Apr 3, 2017 at 17:35

2 Answers 2

4

echo a line after you've read the password:

echo -n "Enter Password:"
read -s password
echo
Sign up to request clarification or add additional context in comments.

Comments

0

I like to take advantage of read's prompt when I am able. Also, it's usually preferable to use printf over echo. For my part I would arrange matters something like this:

read -reps "Enter password:  " 
printf '%s\n' "" "Say what you'd like.  "  "" 

The read command uses -r to suppress any escaped characters (usually a good practice for security reasons); it uses the -p to allow for the prompt which follows in quotes; and it uses -e (for reading by lines) and -s (silence user input).

In this example for printf each pair of quotes appears on a new line (for each string (%s) use a newline (\n)). As above, this would output three lines total. You can build this up as needed. (And if you just want a blank newline somewhere you can add printf '\n' on it's own line.)

If you follow my above read command (at the end of that line) with the name of a password, that's where read will load the user input. Otherwise, as-is, the user input will be stored in the standard REPLY variable. You may (either way) want to unset that variable after you have done with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.