InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Prompting for a Password in bash
Bash

Prompting for a Password in bash

InfinityCoder February 9, 2017

You need to prompt the user for a password, but you don’t want it echoed on the screen.

1
2
read -s -p "password: " PASSWD
printf "%b" "\n"

The -s option tells the read command not to echo the characters typed (s is for silent) and the -p option says that the next argument is the prompt to be displayed prior to reading input.
The line of input that is read from the user is put into the environment variable named $PASSWD.
We follow read with a printf to print out a newline. The printf is necessary because read -s turns off the echoing of characters.

With echoing disabled, when the user presses the Enter key, no newline is echoed and any subsequent output would
appear on the same line as the prompt.

Printing the newline gets us to the next line, as you would expect.

It may even be handy for you to write the code all on one line to avoid intervening logic; putting it on one line also prevents mistakes should you cut and paste this line elsewhere:

1
read -s -p "password: " PASSWD ; printf "%b" "\n"

Be aware that if you read a password into an environment variable it is in memory in plain text, and thus may be accessed via a core dump or /proc/core.

It is also in the process environment, which may be accessible by other processes.

You may be better off using certificates with SSH, if possible.

In any case, it is wise to assume that root and possibly other users on the machine may gain access to the password, so you should handle the situation accordingly.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Using Configuration Files in a Script in bash
You want to use one or more external configuration files …

Using Configuration Files in a Script in bash

Sending Email from Your Script in bash
You’d like your script to be able to send email, …

Sending Email from Your Script in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap