facebook youtube pinterest twitter reddit whatsapp instagram

Suppressing Sensitive Entered Text/String In Bash

So far in our examples or scripts, we have written in the previous guides, we have no way of controlling what is visible to user, which is fine if we have no sensitive data such as passwords.

As soon as you start prompting for sensitive data, it is recommended you suppress or hide the text, and that can be done using the read command with the -s option. Here is a before and after script example:

Before:

#!/bin/bash
read -p "What is Your Name? " name
echo "Your Name is $name"
read -p "Enter Password? " password
read -n1 -p "Press any key to exit "
echo
exit 0

After:

#!/bin/bash
read -p "What is Your Name? " name
echo "Your Name is $name"
read -s -p "Enter Password? " password
echo
read -sn1 -p "Press any key to exit "
echo
exit 0

The read -s option will read password from the user, it will nit display as you type and it would then store it to variable password.

On the line that has "Press any key to exit", we initially didn't use the s option, which means when user enters any key, it would display it before exiting. We tidy it up by adding an s option, now, when we enter any key to continue, it will not be displayed on the screen.

Here is the output of the newly improved script:

user@server:~/bin$ read2.sh
What is Your Name? Mr. Devsrealmer
Your Name is Mr. Devsrealmer
Enter Password?                                    #This won't show anything as you type
Press any key to exit                              #This also won't show anything as you type
pascal@blog:~/bin$

Related Post(s)

  • sed (Stream Editor) In Bash Scripting

    Another powerful filter in GNU/Linux is sed (stream editor), which is a program for performing basic editing tasks on the output of another program or on a file. sed or stream editor can be used to p

  • awk In Bash Scripting

    Up until now, we have covered a couple of text processing, from sed to grep to sort, and now we have the big daddy, which is awk. awk (which stands for Aho, Weinberger, and Kernighan - its creators)

  • (Bash Script) Automate Multiple Classicpress & Wordpress Installation + [Caching and SSL]

    If you are tired of using control panels (I am because they are mostly not secure, and most comes loaded with bundles of useless stacks), and want to do everything on the server level, I wrote a menu

  • Returning Values From Functions in (Bash)

    In the guide function in bash, we dive deep in bash functions and we also pass some information into the functions to carry out a certain operation. In this guide, I'll show you how to get informatio

  • Functions in Bash

    Functions are modular building blocks for creating powerful and modular scripts, using function makes it easy to isolate a code since it would be in a single building block, which would be isolated f

  • grep (Regular Expression) In Bash Scripting

    We recently discussed filters in bash scripting, in this guide we would look more into the practical usage of using a filter program, and an example of such a program is grep. grep, in its simplest f