0
0
Bash Scriptingscripting~5 mins

Silent input with read -s (passwords) in Bash Scripting

Choose your learning style9 modes available
Introduction
Sometimes you need to ask for a password or secret without showing it on the screen. Silent input hides what you type.
When asking a user to enter a password in a script.
When collecting sensitive information like PINs or secret keys.
When you want to keep input private on shared or public computers.
Syntax
Bash Scripting
read -s variable_name
The -s option makes the input silent (no characters shown).
You can add -p to show a prompt message before input, like: read -s -p "Enter password: " password
Examples
Reads input silently and stores it in the variable 'password'. No prompt shown.
Bash Scripting
read -s password
Shows a prompt, reads password silently, then confirms input received.
Bash Scripting
read -s -p "Enter your password: " password

echo "Password received."
Sample Program
This script asks for a password silently, then prints how many characters were typed without showing the password itself.
Bash Scripting
#!/bin/bash

read -s -p "Enter your password: " password

echo
 echo "You entered a password with length: ${#password}"
OutputSuccess
Important Notes
After silent input, use echo to print a newline because the prompt stays on the same line.
Silent input does not show any characters, not even stars (*).
Be careful when using silent input in scripts that log commands or outputs.
Summary
Use 'read -s' to hide user input for passwords or secrets.
Add '-p' to show a prompt message before input.
Silent input keeps sensitive data private on screen.